Ruby:Set的子类生成的断言错误消息 require'set' 需要“测试/单元” 类Foo
预期产出:Ruby:Set的子类生成的断言错误消息 require'set' 需要“测试/单元” 类Foo,ruby,error-handling,set,assert,Ruby,Error Handling,Set,Assert,预期产出: require 'set' require 'test/unit' class Foo < Set def to_s "to_s" end alias_method :inspect, :to_s end class FooTest < Test::Unit::TestCase def test1 assert_equal(Foo.new, false) end end test1(FooTest)[test.rb:12]:预期的,
require 'set'
require 'test/unit'
class Foo < Set
def to_s
"to_s"
end
alias_method :inspect, :to_s
end
class FooTest < Test::Unit::TestCase
def test1
assert_equal(Foo.new, false)
end
end
test1(FooTest)[test.rb:12]:预期的,但实际是。
实际产量:
test1(FooTest) [test.rb:12]: <to_s> expected but was <false>.
test1(FooTest)[test.rb:12]:预期的,但实际是。
编辑
单元使用了一种叫做pretty_inspect的奇怪方法,这是我第一次听说的。尽管如此,本规范仍将按预期工作:
解决方案:
test1(FooTest) [test.rb:12]: <#<Foo: {}>> expected but was <false>.
require'set'
需要“测试/单元”
类Foo
测试/单元似乎不依赖对象的检查方法。查看源代码,它可能直接访问对象的classinspect
方法,但我尝试重新定义Set
sinspect
实例方法也没有成功。检查测试/单元的来源。;-) Max,感谢您澄清这一点。在引用方法(尤其是不熟悉的方法)时,您可能希望使用以下链接包含类或模块:。
require 'set'
require 'test/unit'
class Foo < Set
def to_s
"to_s"
end
alias_method :pretty_inspect, :to_s
end
class FooTest < Test::Unit::TestCase
def test1
assert_equal(Foo.new, false)
end
end