I was happily mocking away with Mocha, then I passed a stub to a case statement at which point I was a little flummoxed.
The Ruby documentation clearly states that the ‘when’ in a ‘case’ statement uses the ‘===’ method for comparing the subject so I couldn’t work out why something like the following wasn’t working:
# Code class A; end def foo(instance) case instance when A : 'an A instance' else 'not an A instance' end end # Test a = stub_everything a.stubs(:===).with(A).returns(true) assert_equal 'an A instance', foo(a)
So I had a little play around in irb and found the following:
class A; end a = A.new a === A #=> false A === a #=> true
This revealed that I was actually stubbing the wrong side of the operator, I changed this to the following and voila!
# Code class A; end def foo(instance) case instance when A : 'an A instance' else 'not an A instance' end end # Test a = stub_everything A.stubs(:===).with(a).returns(true) assert_equal 'an A instance', foo(a)
Looks obvious now but certainly wasn’t at the time!
0 comments ↓
There are no comments yet...Kick things off by filling out the form below.
Leave a Comment