Class: Grift::MockStore

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeGrift::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.

Parameters:

Returns:



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.

Examples:

mock_store = Grift::MockStore.new
mock_store.empty?
#=> true
mock_method = Grift.mock(MyClass, :my_method)
mock_store.store(mock_method)
mock_store.empty?
#=> false

Returns:

  • (Boolean)

    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.

Examples:

mock_store = Grift::MockStore.new
mock_method = Grift.mock(MyClass, :my_method)
mock_store.include?(mock_method)
#=> false
mock_store.store(mock_method)
mock_store.include?(mock_method)
#=> true

Parameters:

Returns:

  • (Boolean)

    if the mock store includes that 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.

Parameters:

  • klass (Class) (defaults to: nil)

    the class to filter by, if nil all classes are included

  • method (Symbol) (defaults to: nil)

    the method to filter by, if nil all symbols are included

Returns:

  • (Array)

    the mocks in the store that match the criteria



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.

Parameters:

  • klass (Class) (defaults to: nil)

    the class to filter by, if nil all classes are included

  • method (Symbol) (defaults to: nil)

    the method to filter by, if nil all symbols are included

Returns:



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.

Examples:

mock_store = Grift::MockStore.new
mock_method = Grift.mock(MyClass, :my_method)
mock_store.store(mock_method)

Parameters:

Returns:

Raises:



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