Module: Grift

Defined in:
lib/grift.rb,
lib/grift/error.rb,
lib/grift/config.rb,
lib/grift/version.rb,
lib/grift/mock_store.rb,
lib/grift/mock_method.rb,
lib/grift/minitest_plugin.rb,
lib/grift/mock_method/mock_executions.rb,
lib/grift/mock_method/mock_executions/mock_arguments.rb

Overview

The base of the gem. Nearly all interactions with Grift should occur through this base module.

Defined Under Namespace

Modules: Config, MinitestPlugin Classes: Error, MockMethod, MockStore

Constant Summary collapse

VERSION =

gem version

'3.2.0'

Class Method Summary collapse

Class Method Details

.clear_all_mocks ⇒ Array<Grift::MockMethod::MockExecutions>

Clear the mocks of methods for all classes. Returns an array of the new state of mock executions for all mocks.

Examples:

Grift.clear_all_mocks

Returns:



147
148
149
# File 'lib/grift.rb', line 147

def clear_all_mocks
  mock_store.mocks.each(&:mock_clear)
end

.clear_mocks(klass) ⇒ Array<Grift::MockMethod::MockExecutions>

Clear the mocks of methods for a single class. Returns an array of the new state of mock executions for the class after clearing.

Examples:

Grift.clear_mocks(MyClass)

Parameters:

  • klass (Class) —

    the class for which to clear mocks

Returns:



134
135
136
# File 'lib/grift.rb', line 134

def clear_mocks(klass)
  mock_store.mocks(klass: klass).each(&:mock_clear)
end

.mock(klass, method, return_value = nil) ⇒ Grift::MockMethod

Mocks the given method to return the provided value. This is syntactical sugar equivalent to calling spy_on and then mock_return_value. To mock an instance method when the class responds to a method of the same name, use spy_on with instance: true.

Examples:

my_mock = Grift.mock(MyClass, :some_method, true)

Parameters:

  • klass (Class) —

    the class of the method to be mocked

  • method (Symbol) —

    the symbol representing the method to be mocked

  • return_value (defaults to: nil) —

    the value the method should return while mocked

Returns:

Raises:

  • (Grift::Error) —

    exception if method is already mocked or does not exist



43
44
45
# File 'lib/grift.rb', line 43

def mock(klass, method, return_value = nil)
  spy_on(klass, method).mock_return_value(return_value)
end

.mock_method?(klass, method) ⇒ Boolean

Checks whether the given method is currently mocked.

Examples:

Grift.mock(String, :upcase, 'STRING')
Grift.mock_method?(String, :upcase)
#=> true
Grift.mock_method?(String, :downcase)
#=> false

Parameters:

  • klass (Class) —

    the class of the method to be checked

  • method (Symbol) —

    the symbol representing the method to be checked

Returns:

  • (Boolean) —

    true if method is currently mocked



91
92
93
94
# File 'lib/grift.rb', line 91

def mock_method?(klass, method)
  hash_key = Grift::MockMethod.hash_key(klass, method)
  mock_store.include?(hash_key)
end

.mock_store ⇒ Grift::MockStore

Returns the current store of mocked methods scoped to the thread.

Examples:

Grift.mock_store

Returns:

  • (Grift::MockStore) —

    the current store of mocked methods scoped to the thread



21
22
23
# File 'lib/grift.rb', line 21

def mock_store
  Thread.current[:grift_internal_mock_store] ||= Grift::MockStore.new
end

.reset_all_mocks ⇒ Array<Grift::MockMethod::MockExecutions>

Reset the mocks of methods for all classes. Returns an array of the new state of mock executions for all mocks.

Examples:

Grift.reset_all_mocks

Returns:



175
176
177
# File 'lib/grift.rb', line 175

def reset_all_mocks
  mock_store.mocks.each(&:mock_reset)
end

.reset_mocks(klass) ⇒ Array<Grift::MockMethod::MockExecutions>

Reset the mocks of methods for a single class. Returns an array of the new state of mock executions for the class after resetting.

Examples:

Grift.reset_mocks(MyClass)

Parameters:

  • klass (Class) —

    the class for which to reset mocks

Returns:



162
163
164
# File 'lib/grift.rb', line 162

def reset_mocks(klass)
  mock_store.mocks(klass: klass).each(&:mock_reset)
end

.restore_all_mocks(watch: false) ⇒ Array<Grift::MockMethod::MockExecutions>, Grift::MockStore

Restore the mocks of methods for all classes. Returns an array of the new state of mock executions for all mocks.

Examples:

Grift.restore_mocks
#=> restores all mocks and stops watching them
Grift.restore_mocks(watch: true)
#=> restores all mocks but keeps watching them

Parameters:

  • watch (Boolean) (defaults to: false) —

    if true, keep watching the methods

Returns:



218
219
220
221
222
223
224
# File 'lib/grift.rb', line 218

def restore_all_mocks(watch: false)
  if watch
    mock_store.mocks.each { |m| m.mock_restore(watch: true) }
  else
    mock_store.remove
  end
end

.restore_mocks(klass, watch: false) ⇒ Array<Grift::MockMethod::MockExecutions>

Restore the mocks of methods for a single class. Returns an array of the new state of mock executions for the class after restoring.

Examples:

Grift.restore_mocks(MyClass)
#=> restores mocks and stops watching them
Grift.restore_mocks(MyClass, watch: true)
#=> restores mocks but keeps watching them

Parameters:

  • klass (Class) —

    the class for which to restore mocks

  • watch (Boolean) (defaults to: false) —

    if true, keep watching the methods

Returns:



195
196
197
198
199
200
201
# File 'lib/grift.rb', line 195

def restore_mocks(klass, watch: false)
  if watch
    mock_store.mocks(klass: klass).each { |m| m.mock_restore(watch: true) }
  else
    mock_store.remove(klass: klass)
  end
end

.restricted_method?(klass, method) ⇒ Boolean

Checks whether the given method is restricted from being mocked. For the list of restricted methods see Grift::Config.restricted_methods.

Examples:

Grift.restricted_method?(Grift, :mock)
#=> true
Grift.restricted_method?(String, :upcase)
#=> false

Parameters:

  • klass (Class) —

    the class of the method to be checked

  • method (Symbol) —

    the symbol representing the method to be checked

Returns:

  • (Boolean) —

    true if method cannot be mocked by Grift

See Also:



115
116
117
118
119
120
121
# File 'lib/grift.rb', line 115

def restricted_method?(klass, method)
  base_klass = klass.to_s.split('::').first
  klass_config = Grift::Config.restricted_methods[base_klass]
  return false unless klass_config

  (klass_config.include?('*') && !klass_config.include?("^#{method}")) || klass_config.include?(method.to_s)
end

.spy_on(klass, method, instance: false) ⇒ Grift::MockMethod

Creates a mock for the given method without mocking the implementation or return values.

If the class responds to the method itself, the class method is watched. This includes methods every class has, like name, to_s, and ==. Pass instance: true to watch the instance method of the same name instead.

Examples:

my_mock = Grift.spy_on(MyClass, :some_method)
my_mock = Grift.spy_on(MyClass, :name, instance: true)

Parameters:

  • klass (Class) —

    the class of the method to be watched

  • method (Symbol) —

    the symbol representing the method to be watched

  • instance (Boolean) (defaults to: false) —

    if true, watch the instance method even if the class responds to the method itself

Returns:

Raises:

  • (Grift::Error) —

    exception if method is already mocked or does not exist



70
71
72
# File 'lib/grift.rb', line 70

def spy_on(klass, method, instance: false)
  MockMethod.new(klass, method, instance: instance)
end