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
-
.clear_all_mocks ⇒ Array<Grift::MockMethod::MockExecutions>
Clear the mocks of methods for all classes.
-
.clear_mocks(klass) ⇒ Array<Grift::MockMethod::MockExecutions>
Clear the mocks of methods for a single class.
-
.mock(klass, method, return_value = nil) ⇒ Grift::MockMethod
Mocks the given method to return the provided value.
-
.mock_method?(klass, method) ⇒ Boolean
Checks whether the given method is currently mocked.
-
.mock_store ⇒ Grift::MockStore
The current store of mocked methods scoped to the thread.
-
.reset_all_mocks ⇒ Array<Grift::MockMethod::MockExecutions>
Reset the mocks of methods for all classes.
-
.reset_mocks(klass) ⇒ Array<Grift::MockMethod::MockExecutions>
Reset the mocks of methods for a single class.
-
.restore_all_mocks(watch: false) ⇒ Array<Grift::MockMethod::MockExecutions>, Grift::MockStore
Restore the mocks of methods for all classes.
-
.restore_mocks(klass, watch: false) ⇒ Array<Grift::MockMethod::MockExecutions>
Restore the mocks of methods for a single class.
-
.restricted_method?(klass, method) ⇒ Boolean
Checks whether the given method is restricted from being mocked.
-
.spy_on(klass, method, instance: false) ⇒ Grift::MockMethod
Creates a mock for the given method without mocking the implementation or return values.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
70 71 72 |
# File 'lib/grift.rb', line 70 def spy_on(klass, method, instance: false) MockMethod.new(klass, method, instance: instance) end |