Module: Grift::MinitestPlugin
- Defined in:
- lib/grift/minitest_plugin.rb
Overview
A plugin to clean up mocks after tests in Minitest and to assert on mocks. It is recommended that you include this plugin to avoid needing to cleanup the mocks after each test.
To setup the plugin for your tests, you should include the
following code in your test_helper
class Minitest::Test
include Grift::MinitestPlugin
end
Instance Method Summary collapse
-
#after_teardown ⇒ Grift::MockStore
After each test restores all mocks with no watching.
-
#assert_mock_called(mock, msg = nil) ⇒ true
Asserts that the mocked method has been called at least once.
-
#assert_mock_called_with(mock, *args, **kwargs) ⇒ true
Asserts that the mocked method has been called at least once with exactly the given arguments.
-
#assert_mocked(klass, method, msg = nil) ⇒ true
Asserts that the given method is currently mocked.
-
#refute_mock_called(mock, msg = nil) ⇒ true
Asserts that the mocked method has not been called.
-
#refute_mock_called_with(mock, *args, **kwargs) ⇒ true
Asserts that the mocked method has not been called with exactly the given arguments.
-
#refute_mocked(klass, method, msg = nil) ⇒ true
Asserts that the given method is not currently mocked.
Instance Method Details
#after_teardown ⇒ Grift::MockStore
After each test restores all mocks with no watching.
22 23 24 25 |
# File 'lib/grift/minitest_plugin.rb', line 22 def after_teardown super Grift.restore_all_mocks(watch: false) end |
#assert_mock_called(mock, msg = nil) ⇒ true
Asserts that the mocked method has been called at least once.
79 80 81 |
# File 'lib/grift/minitest_plugin.rb', line 79 def assert_mock_called(mock, msg = nil) refute mock.empty?, (msg) { "Expected #{mock} to be called" } end |
#assert_mock_called_with(mock, *args, **kwargs) ⇒ true
Asserts that the mocked method has been called at least once with exactly the given arguments. Every argument is matched against the calls, so there is no custom message parameter.
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/grift/minitest_plugin.rb', line 117 def assert_mock_called_with(mock, *args, **kwargs) failure = proc do expected = "Expected #{mock} to be called with #{mock_call_description(args, kwargs)}" if mock.empty? "#{expected}, but it was not called." else "#{expected}, but it was called with:\n#{mock_calls_description(mock)}" end end assert mock_called_with?(mock, args, kwargs), failure end |
#assert_mocked(klass, method, msg = nil) ⇒ true
Asserts that the given method is currently mocked.
42 43 44 45 |
# File 'lib/grift/minitest_plugin.rb', line 42 def assert_mocked(klass, method, msg = nil) key = Grift::MockMethod.hash_key(klass, method) assert Grift.mock_method?(klass, method), (msg) { "Expected #{key} to be mocked" } end |
#refute_mock_called(mock, msg = nil) ⇒ true
Asserts that the mocked method has not been called.
95 96 97 98 99 100 |
# File 'lib/grift/minitest_plugin.rb', line 95 def refute_mock_called(mock, msg = nil) failure = (msg, '') do "Expected #{mock} to not be called, but it was called with:\n#{mock_calls_description(mock)}" end assert mock.empty?, failure end |
#refute_mock_called_with(mock, *args, **kwargs) ⇒ true
Asserts that the mocked method has not been called with exactly the given arguments. Every argument is matched against the calls, so there is no custom message parameter.
144 145 146 147 |
# File 'lib/grift/minitest_plugin.rb', line 144 def refute_mock_called_with(mock, *args, **kwargs) failure = proc { "Expected #{mock} to not be called with #{mock_call_description(args, kwargs)}." } refute mock_called_with?(mock, args, kwargs), failure end |
#refute_mocked(klass, method, msg = nil) ⇒ true
Asserts that the given method is not currently mocked.
61 62 63 64 |
# File 'lib/grift/minitest_plugin.rb', line 61 def refute_mocked(klass, method, msg = nil) key = Grift::MockMethod.hash_key(klass, method) refute Grift.mock_method?(klass, method), (msg) { "Expected #{key} to not be mocked" } end |