我试图用ruby对一个类的两个实例进行单元测试:

def test_example
    a = Object.new
    b = Object.new

    assert_equal a, b
end

我知道这是失败的,因为实例是不同的变量,每个都有自己的内存指针。我要做的是让这个测试通过,如果实例在所有方面都相同,除了它们的引用指针。
下面是一个更为复杂(如果是人为的)的例子:
# Let's stir up something...
class FooBar
    attr_accessor :seed, :foo, :bar

    def foo_the_bar()
        @bar = @seed + @foo * 3
    end
end

f = FooBar.new
f.seed = "Mountains "
f.foo  = "are just mountains "
f.bar = "obliterate me!"
f.foo_the_bar
p f.bar # "Mountains are just mountains are just mountains are just mountains "


# So far so good, now let's test if one FooBar is same as another...

require 'test/unit'
class FooBarTest < Test::Unit::TestCase

    # Test fails: <#<FooBar:0x9a40d18>> expected but was <#<FooBar:0x9a40d04>>.
    # But what we really want is a way to make this pass
    # since the objects are exactly the same in every respect,
    # besides their pointers.
    def test_foobar_1_init
        f1 = FooBar.new
        f2 = FooBar.new

        assert_equal f1, f2
    end

    # Again, test fails, but we want it to pass,
    # since the instance variables are the same.
    def test_foobar_2_instance_var
        f1 = FooBar.new
        f2 = FooBar.new

        f1.seed = "Santa says "
        f1.foo = "ho "
        f1.bar = "something"
        f1.foo_the_bar

        f2.seed = f1.seed
        f2.foo = f1.foo
        f2.foo_the_bar

        assert_equal f1, f2
    end

    # This test fails for the wrong reason.
    # We want this to fail because the instance variables are different.
    # This test should also fail if we add a method to one of the instances,
    # or make the instances differ from each some other way.
    def test_foobar_3_diff
        f1 = FooBar.new
        f2 = FooBar.new

        f1.seed = "Kitty goes "
        f1.foo = "meow "
        f1.foo_the_bar

        f2.seed = "Doggies goes "
        f2.foo = "woof "
        f2.foo_the_bar

        assert_equal f1, f2
    end
end

最佳答案

f1.attributes.keys.collect(&:to_sym).each do |field|
 assert_equal f1.send(field), f2.send(field)
end

这将断言所有字段的相等性。但缺点是断言的数量。如果不希望发生这种情况,请将ID分配给这样的对象
f1.id = 1
f2.id = 1
assert_equal f1, f2

但请确保不要保存可能导致不一致的对象。

关于ruby - 如何在Ruby中对类实例的相同性/相等性进行单元测试?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13152158/

10-09 05:40
查看更多