Created
November 17, 2017 12:09
-
-
Save lcguida/269d0185cb36d8f3f35912e07dad6a7b to your computer and use it in GitHub Desktop.
Minitest `must_call`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Wrapper to mock.verify procedure when asserting | |
# a method is called in minitest | |
class Object | |
def must_call(method_name, returns: nil, arguments: []) | |
mock = Minitest::Mock.new | |
mock.expect(:call, returns, arguments) | |
self.stub(method_name, mock) do | |
yield | |
end | |
begin | |
mock.verify.must_equal true | |
rescue MockExpectationError | |
# We want a better message than 'expected call() => nil' | |
raise MockExpectationError, | |
"Expected #{self.inspect} to call #{method_name}(#{arguments.inspect}) => #{returns.inspect}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment