test/unit appears to not rely on an object's to_s
or inspect
method. Looking at the source, it may be accessing the object's class inspect
method directly, but my attempts to redefine Set
s inspect
instance method didn't work either. Check out the source of test/unit. ;-)
Ruby: assert error message produced by a subclass of Set
-
23-09-2022 - |
Question
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
Expected output:
test1(FooTest) [test.rb:12]: <to_s> expected but was <false>.
Actual output:
test1(FooTest) [test.rb:12]: <#<Foo: {}>> expected but was <false>.
Edit
Test::Unit uses a strange method called pretty_inspect, which I hear of the first time. Nevertheless, this code will work as expected:
Solution:
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
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow