Search code examples
minitest

Does verifying the mock is need in minitest


I'm reading the mocking capability of minitest.

require "minitest/autorun"

mock = MiniTest::Mock.new
mock.expect(:use_any_string, true, [String])
mock.use_any_string("foo")
## mock.use_any_string(1)
## MockExpectationError: mocked method :use_any_string called with unexpected arguments [1]


## I do not understand the purpose for this 
mock.verify

So I do not understand the purpose of using mock.verify since the trying to pass any another type(to use_any_string) other than String result in mock expectation error.

So why should one use assert mock.verify then?


Solution

  • You are right, you cannot set anything else to the configured mock, but if your mock is not called at all, then you can find out with mock.verify. So if your method under test should call the mocked method, you should verify that it was called at all.