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(matcher = nil, &time_condition_block) ⇒ TimesCalledExpectation

Returns a new instance of TimesCalledExpectation.

Raises:

  • (ArgumentError)


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

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

Instance Attribute Details

#matcherObject (readonly)

Returns the value of attribute matcher.



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

def matcher
  @matcher
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

#attempt!Object



18
19
20
21
22
# File 'lib/rr/expectations/times_called_expectation.rb', line 18

def attempt!
  @times_called += 1
  verify_input_error unless @matcher.possible_match?(@times_called)
  return
end

#attempt?Boolean

Returns:

  • (Boolean)


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

def attempt?
  @matcher.attempt?(@times_called)
end

#verifyObject



24
25
26
27
# File 'lib/rr/expectations/times_called_expectation.rb', line 24

def verify
  return false unless @matcher.is_a?(TimesCalledMatchers::TimesCalledMatcher)
  return @matcher.matches?(@times_called)
end

#verify!Object



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

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