Class: Crspec::Mock::Space

Inherits:
Object
  • Object
show all
Defined in:
lib/crspec/mock/space.rb

Constant Summary collapse

STORAGE_KEY =
:crspec_mock_space

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpace

Returns a new instance of Space.



26
27
28
29
30
31
32
# File 'lib/crspec/mock/space.rb', line 26

def initialize
  @doubles = []
  @stubs = Hash.new { |h, k| h[k] = {} }
  @expectations = []
  @calls = Hash.new { |h, k| h[k] = [] }
  @mutex = Mutex.new
end

Class Method Details

.currentObject



10
11
12
# File 'lib/crspec/mock/space.rb', line 10

def self.current
  Fiber[STORAGE_KEY] ||= new
end

.verify_and_reset!Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/crspec/mock/space.rb', line 14

def self.verify_and_reset!
  space = Fiber[STORAGE_KEY]
  return unless space

  begin
    space.verify!
  ensure
    space.reset!
    Fiber[STORAGE_KEY] = nil
  end
end

Instance Method Details

#calls_for(target, method_name) ⇒ Object



40
41
42
43
44
# File 'lib/crspec/mock/space.rb', line 40

def calls_for(target, method_name)
  @mutex.synchronize do
    @calls[[target.object_id, method_name.to_sym]].dup
  end
end

#fetch_stub(target, method_name) ⇒ Object



54
55
56
57
58
# File 'lib/crspec/mock/space.rb', line 54

def fetch_stub(target, method_name)
  @mutex.synchronize do
    @stubs[target.object_id][method_name.to_sym]
  end
end

#record_call(target, method_name, args, kwargs) ⇒ Object



34
35
36
37
38
# File 'lib/crspec/mock/space.rb', line 34

def record_call(target, method_name, args, kwargs)
  @mutex.synchronize do
    @calls[[target.object_id, method_name.to_sym]] << [args, kwargs]
  end
end

#register_double(double_obj) ⇒ Object



66
67
68
69
70
# File 'lib/crspec/mock/space.rb', line 66

def register_double(double_obj)
  @mutex.synchronize do
    @doubles << double_obj
  end
end

#register_expectation(expectation) ⇒ Object



60
61
62
63
64
# File 'lib/crspec/mock/space.rb', line 60

def register_expectation(expectation)
  @mutex.synchronize do
    @expectations << expectation
  end
end

#register_stub(target, method_name, implementation) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/crspec/mock/space.rb', line 46

def register_stub(target, method_name, implementation)
  method_sym = method_name.to_sym
  ensure_interceptor_prepended(target, method_sym)
  @mutex.synchronize do
    @stubs[target.object_id][method_sym] = implementation
  end
end

#reset!Object



77
78
79
80
81
82
83
84
# File 'lib/crspec/mock/space.rb', line 77

def reset!
  @mutex.synchronize do
    @stubs.clear
    @doubles.clear
    @expectations.clear
    @calls.clear
  end
end

#verify!Object



72
73
74
75
# File 'lib/crspec/mock/space.rb', line 72

def verify!
  @expectations.each(&:verify_expectations!)
  @doubles.each(&:verify_expectations!)
end