Class: RR::Expectations::TimesCalledExpectation

Inherits:
Object
  • Object
show all
Defined in:
lib/rr/expectations/times_called_expectation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(times = nil, &time_condition_block) ⇒ TimesCalledExpectation

Returns a new instance of TimesCalledExpectation.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
# File 'lib/rr/expectations/times_called_expectation.rb', line 6

def initialize(times=nil, &time_condition_block)
  raise ArgumentError, "Cannot pass in both an argument and a block" if times && time_condition_block
  @times = times || time_condition_block
  @times_called = 0
  @verify_backtrace = caller[1..-1]
end

Instance Attribute Details

#timesObject (readonly)

Returns the value of attribute times.



4
5
6
# File 'lib/rr/expectations/times_called_expectation.rb', line 4

def times
  @times
end

#times_calledObject (readonly)

Returns the value of attribute times_called.



4
5
6
# File 'lib/rr/expectations/times_called_expectation.rb', line 4

def times_called
  @times_called
end

Instance Method Details

#verifyObject



20
21
22
23
24
25
# File 'lib/rr/expectations/times_called_expectation.rb', line 20

def verify
  return true if @times.is_a?(Integer) && @times == @times_called
  return true if @times.is_a?(Proc) && @times.call(@times_called)
  return true if @times.is_a?(Range) && @times.include?(@times_called)
  return false
end

#verify!Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rr/expectations/times_called_expectation.rb', line 27

def verify!
  unless verify
    if @verify_backtrace
      error = Errors::TimesCalledError.new(error_message)
      error.backtrace = @verify_backtrace
      raise error
    else
      raise Errors::TimesCalledError, error_message
    end
  end
end

#verify_inputObject



13
14
15
16
17
18
# File 'lib/rr/expectations/times_called_expectation.rb', line 13

def verify_input
  @times_called += 1
  verify_input_error if @times.is_a?(Integer) && @times_called > @times
  verify_input_error if @times.is_a?(Range) && @times_called > @times.end
  return
end