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

'2.2.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.mock_storeGrift::MockStore (readonly)

Returns the current store of mocked methods.

Examples:

Grift.mock_store

Returns:



23
24
25
# File 'lib/grift.rb', line 23

def mock_store
  @mock_store
end

Class Method Details

.clear_all_mocksArray<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:



136
137
138
# File 'lib/grift.rb', line 136

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:



123
124
125
# File 'lib/grift.rb', line 123

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`.

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



41
42
43
# File 'lib/grift.rb', line 41

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



80
81
82
83
# File 'lib/grift.rb', line 80

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

.reset_all_mocksArray<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:



164
165
166
# File 'lib/grift.rb', line 164

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:



151
152
153
# File 'lib/grift.rb', line 151

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:



207
208
209
210
211
212
213
# File 'lib/grift.rb', line 207

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:



184
185
186
187
188
189
190
# File 'lib/grift.rb', line 184

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:



104
105
106
107
108
109
110
# File 'lib/grift.rb', line 104

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) ⇒ Grift::MockMethod

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

Examples:

my_mock = Grift.spy_on(MyClass, :some_method)

Parameters:

  • klass (Class)

    the class of the method to be watched

  • method (Symbol)

    the symbol representing the method to be watched

Returns:

Raises:

  • (Grift::Error)

    exception if method is already mocked or does not exist



59
60
61
# File 'lib/grift.rb', line 59

def spy_on(klass, method)
  MockMethod.new(klass, method)
end