Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby:Set的子类生成的断言错误消息 require'set' 需要“测试/单元” 类Foo_Ruby_Error Handling_Set_Assert - Fatal编程技术网

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
测试/单元似乎不依赖对象的
检查方法。查看源代码,它可能直接访问对象的class
inspect
方法,但我尝试重新定义
Set
s
inspect
实例方法也没有成功。检查测试/单元的来源。;-)

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