Skip to content

Instantly share code, notes, and snippets.

@lcguida
Created November 17, 2017 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcguida/269d0185cb36d8f3f35912e07dad6a7b to your computer and use it in GitHub Desktop.
Save lcguida/269d0185cb36d8f3f35912e07dad6a7b to your computer and use it in GitHub Desktop.
Minitest `must_call`
# 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