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
What is Minitest? Minitest is a testing tool for Ruby that provides a complete suite of testing facilities. It also supports behaviour-driven development, mocking and benchmarking. With the release of Ruby 1.9, it was added to Ruby's standard library, which increased its popularity.
You use mocks to test the interaction between two objects. Instead of testing the output value, like in a regular expectation. For example: You're writing an API that flips images. Instead of writing your own image-manipulation code you use a gem like mini_magick .
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 end end
Unfortunately this feature is not documented very well. I found about it from here: https://github.com/seattlerb/minitest/issues/216
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With