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
# File 'lib/crspec/mock/space.rb', line 26

def initialize
  @doubles = []
  @stubs = Hash.new { |h, k| h[k] = {} }
  @expectations = []
  @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

#fetch_stub(target, method_name) ⇒ Object



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

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

#register_double(double_obj) ⇒ Object



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

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

#register_expectation(expectation) ⇒ Object



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

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

#register_stub(target, method_name, implementation) ⇒ Object



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

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



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

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

#verify!Object



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

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