How to assert certain method is called with Ruby minitest framework?
With minitest you use expect
method to set the expectation for a method to be called on a mock object like so
obj = MiniTest::Mock.newobj.expect :right
If you want to set expectation with parameters and return values then:
obj.expect :right, return_value, parameters
And for the concrete object like so:
obj = SomeClass.newassert_send([obj, :right, *parameters])
Minitest has a special .expect :call
for checking if some method is called.
describe SomeClass do it "should invoke right function" do mocked_method = MiniTest::Mock.new mocked_method.expect :call, return_value, [] some_instance = SomeClass.new some_instance.stub :right, mocked_method do some_instance.invoke_function("right") end mocked_method.verify endend
Unfortunately this feature is not documented very well. I found about it from here: https://github.com/seattlerb/minitest/issues/216
According to the given example, there is no other delegate class, and you want to make sure the method is called properly from the same class. Then below code snippet should work:
class SomeTest < Minitest::Test def setup @obj = SomeClass.new end def test_right_method_is_called @obj.stub :right, true do @obj.stub :wrong, false do assert(@obj.invoke_function('right')) end end end def test_wrong_method_is_called @obj.stub :right, false do @obj.stub :wrong, true do assert(@obj.invoke_function('other')) end end endend
The idea is to stub [method_expect_to_be_called] by returning a simple true value, and in the stub block assert it's indeed being called and returning the true value. To stub the other unexpected method is just to make sure that it's not being called.
Note: assert_send won't work correctly. Please refer to official doc.
In fact, below statement will pass, but doesn't mean it's working as expected:
assert_send([obj, :invoke_function, 'right'])# it's just calling invoke_function method, but not verifying any method is being called