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
-
.mock_store ⇒ Grift::MockStore
readonly
The current store of mocked methods.
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.
-
.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) ⇒ Grift::MockMethod
Creates a mock for the given method without mocking the implementation or return values.
Class Attribute Details
.mock_store ⇒ Grift::MockStore (readonly)
Returns the current store of mocked methods.
23 24 25 |
# File 'lib/grift.rb', line 23 def mock_store @mock_store end |
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.
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.
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`.
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.
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_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.
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.
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.
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.
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.
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.
59 60 61 |
# File 'lib/grift.rb', line 59 def spy_on(klass, method) MockMethod.new(klass, method) end |