Class: SimpleMock::Tracer

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_mock/tracer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTracer

Returns a new instance of Tracer.



5
6
7
8
# File 'lib/simple_mock/tracer.rb', line 5

def initialize
  self.actual_calls   = {}
  self.expected_calls = {}
end

Instance Attribute Details

#actual_callsObject

Returns the value of attribute actual_calls.



3
4
5
# File 'lib/simple_mock/tracer.rb', line 3

def actual_calls
  @actual_calls
end

#expected_callsObject

Returns the value of attribute expected_calls.



3
4
5
# File 'lib/simple_mock/tracer.rb', line 3

def expected_calls
  @expected_calls
end

Instance Method Details

#assert(name, actual_args = []) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/simple_mock/tracer.rb', line 10

def assert name, actual_args = []
  expected_args = expected_calls[name]
  unless expected_args.size == actual_args.size
    raise ArgumentError, "mocked method #{name.inspect} expects #{expected_args.size} arguments, got #{actual_args.size}"
  end
  unless expected_args.zip(actual_args).all? { |e, a| e === a || e == a }
    raise MockExpectationError, "mocked method #{name.inspect} called with unexpected arguments #{actual_args.inspect}"
  end
  actual_calls[name] = true
end

#register(name, args = []) ⇒ Object



21
22
23
# File 'lib/simple_mock/tracer.rb', line 21

def register name, args = []
  expected_calls[name] = args
end

#verifyObject

Verify that all methods were called as expected. Raises MockExpectationError if not called as expected.



27
28
29
30
31
32
33
34
# File 'lib/simple_mock/tracer.rb', line 27

def verify
  differences = expected_calls.keys - actual_calls.keys
  if differences.any?
    raise MockExpectationError, "expected #{differences.first.inspect}"
  else
    true
  end
end