Class: Grift::MockStore
- Inherits:
-
Object
- Object
- Grift::MockStore
- Defined in:
- lib/grift/mock_store.rb
Overview
A hash wrapper that tracks what methods are mocked and facilitates cleanup of mocks. This class is meant for internal use and should generally not be called explcitly.
Instance Method Summary collapse
-
#delete(mock_method) ⇒ Grift::MockStore
Unmocks and removes the mock in the store.
-
#empty? ⇒ Boolean
Checks if the mock store is empty.
-
#include?(mock_method) ⇒ Boolean
Checks if the mock store includes the given mock method.
-
#initialize ⇒ Grift::MockStore
constructor
A new instance of MockStore.
-
#mocks(klass: nil, method: nil) ⇒ Array
Searches for the mock in the store.
-
#remove(klass: nil, method: nil) ⇒ Grift::MockStore
Unmocks and removes mocks in the store.
-
#store(mock_method) ⇒ Grift::MockMethod
Add the given mock to the mock store.
Constructor Details
#initialize ⇒ Grift::MockStore
A new instance of MockStore
11 12 13 |
# File 'lib/grift/mock_store.rb', line 11 def initialize @mocks = {} end |
Instance Method Details
#delete(mock_method) ⇒ Grift::MockStore
Unmocks and removes the mock in the store. If the mock is not in the store, nothing will change.
77 78 79 80 81 82 83 84 |
# File 'lib/grift/mock_store.rb', line 77 def delete(mock_method) if include?(mock_method) mock_method.mock_restore(watch: false) @mocks.delete(mock_method.to_s) end self end |
#empty? ⇒ Boolean
Checks if the mock store is empty.
120 121 122 |
# File 'lib/grift/mock_store.rb', line 120 def empty? @mocks.empty? end |
#include?(mock_method) ⇒ Boolean
Checks if the mock store includes the given mock method.
102 103 104 |
# File 'lib/grift/mock_store.rb', line 102 def include?(mock_method) @mocks.include?(mock_method.to_s) end |
#mocks(klass: nil, method: nil) ⇒ Array
Searches for the mock in the store. Optional filtering by class/method. If no parameters are passed in, this returns all mocks in the store.
46 47 48 |
# File 'lib/grift/mock_store.rb', line 46 def mocks(klass: nil, method: nil) search(klass: klass, method: method).values end |
#remove(klass: nil, method: nil) ⇒ Grift::MockStore
Unmocks and removes mocks in the store. Optional filtering by class/method. If no parameters are passed in, cleans up all mocks in the store.
59 60 61 62 63 64 65 66 67 |
# File 'lib/grift/mock_store.rb', line 59 def remove(klass: nil, method: nil) to_remove = search(klass: klass, method: method) to_remove.each do |key, mock| mock.mock_restore(watch: false) @mocks.delete(key) end self end |
#store(mock_method) ⇒ Grift::MockMethod
Add the given mock to the mock store.
30 31 32 33 34 35 |
# File 'lib/grift/mock_store.rb', line 30 def store(mock_method) raise(Grift::Error, 'Must only store Grift Mocks') unless mock_method.instance_of?(Grift::MockMethod) raise(Grift::Error, 'Store aready contains that mock') if include?(mock_method) @mocks[mock_method.to_s] = mock_method end |