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

Instance Method Details

#after_teardown ⇒ Grift::MockStore

After each test restores all mocks with no watching.

Returns:



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.

Examples:

my_mock = Grift.spy_on(MyClass, :my_method)
MyClass.my_method
assert_mock_called(my_mock)

Parameters:

  • mock (Grift::MockMethod) —

    the mock to be checked

  • msg (String, Proc) (defaults to: nil) —

    a message to show before the failure message

Returns:

  • (true)


79
80
81
# File 'lib/grift/minitest_plugin.rb', line 79

def assert_mock_called(mock, msg = nil)
  refute mock.empty?, message(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.

Examples:

my_mock = Grift.spy_on(MyClass, :my_method)
MyClass.my_method('first', second: 2)
assert_mock_called_with(my_mock, 'first', second: 2)

Parameters:

  • mock (Grift::MockMethod) —

    the mock to be checked

  • args (Array) —

    the expected positional arguments

  • kwargs (Hash) —

    the expected keyword arguments

Returns:

  • (true)


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.

Examples:

Grift.spy_on(MyClass, :my_method)
assert_mocked(MyClass, :my_method)

Parameters:

  • klass (Class) —

    the class of the method to be checked

  • method (Symbol) —

    the symbol representing the method to be checked

  • msg (String, Proc) (defaults to: nil) —

    a message to show before the failure message

Returns:

  • (true)

See Also:



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), message(msg) { "Expected #{key} to be mocked" }
end

#refute_mock_called(mock, msg = nil) ⇒ true

Asserts that the mocked method has not been called.

Examples:

my_mock = Grift.spy_on(MyClass, :my_method)
refute_mock_called(my_mock)

Parameters:

  • mock (Grift::MockMethod) —

    the mock to be checked

  • msg (String, Proc) (defaults to: nil) —

    a message to show before the failure message

Returns:

  • (true)


95
96
97
98
99
100
# File 'lib/grift/minitest_plugin.rb', line 95

def refute_mock_called(mock, msg = nil)
  failure = message(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.

Examples:

my_mock = Grift.spy_on(MyClass, :my_method)
MyClass.my_method('first', second: 2)
refute_mock_called_with(my_mock, 'first', second: 3)

Parameters:

  • mock (Grift::MockMethod) —

    the mock to be checked

  • args (Array) —

    the positional arguments that should not have been used

  • kwargs (Hash) —

    the keyword arguments that should not have been used

Returns:

  • (true)


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.

Examples:

refute_mocked(MyClass, :my_method)

Parameters:

  • klass (Class) —

    the class of the method to be checked

  • method (Symbol) —

    the symbol representing the method to be checked

  • msg (String, Proc) (defaults to: nil) —

    a message to show before the failure message

Returns:

  • (true)

See Also:



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), message(msg) { "Expected #{key} to not be mocked" }
end