I want to test whether a function invokes other functions properly with minitest Ruby, but I cannot find a proper assert
to test from the doc.
class SomeClass
def invoke_function(name)
name == "right" ? right () : wrong ()
end
def right
#...
end
def wrong
#...
end
end
The test code:
describe SomeClass do
it "should invoke right function" do
# assert right() is called
end
it "should invoke other function" do
# assert wrong() is called
end
end
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:
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:
With minitest you use
expect
method to set the expectation for a method to be called on a mock object like soIf you want to set expectation with parameters and return values then:
And for the concrete object like so:
Minitest has a special
.expect :call
for checking if some method is called.Unfortunately this feature is not documented very well. I found about it from here: https://github.com/seattlerb/minitest/issues/216