Class: Mocha::Expectation

Inherits:
Object show all
Defined in:
lib/mocha/expectation.rb

Direct Known Subclasses

MissingExpectation, Stub

Defined Under Namespace

Classes: AlwaysEqual, InvalidExpectation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name) ⇒ Expectation

Returns a new instance of Expectation.



20
21
22
23
24
25
# File 'lib/mocha/expectation.rb', line 20

def initialize(method_name)
  @method_name = method_name
  @count = 1
  @parameters, @parameter_block = AlwaysEqual.new, nil
  @invoked, @return_value = 0, nil
end

Instance Attribute Details

#method_nameObject (readonly)

Returns the value of attribute method_name.



18
19
20
# File 'lib/mocha/expectation.rb', line 18

def method_name
  @method_name
end

Instance Method Details

#at_least(minimum) ⇒ Object



44
45
46
47
# File 'lib/mocha/expectation.rb', line 44

def at_least(minimum)
  times(Range.at_least(minimum))
  self
end

#at_least_onceObject



49
50
51
52
# File 'lib/mocha/expectation.rb', line 49

def at_least_once()
  at_least(1)
  self
end

#invokeObject



70
71
72
73
# File 'lib/mocha/expectation.rb', line 70

def invoke
  @invoked += 1
  @return_value.is_a?(Proc) ? @return_value.call : @return_value
end

#match?(method_name, *arguments) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'lib/mocha/expectation.rb', line 27

def match?(method_name, *arguments)
  if @parameter_block then
    @parameter_block.call(*arguments)
  else
    (@method_name == method_name) and (@parameters == arguments)
  end
end

#messageObject



81
82
83
84
85
# File 'lib/mocha/expectation.rb', line 81

def message
  params = @parameters.is_a?(Array) ? @parameters : [@parameters.to_s]
  params = PrettyParameters.new(params)
  ":#{@method_name}(#{params.pretty})"
end

#neverObject



40
41
42
# File 'lib/mocha/expectation.rb', line 40

def never
  times(0)
end

#raises(exception = RuntimeError, message = nil) ⇒ Object



65
66
67
68
# File 'lib/mocha/expectation.rb', line 65

def raises(exception = RuntimeError, message = nil)
  @return_value = lambda{ raise exception, message }
  self
end

#returns(value) ⇒ Object



60
61
62
63
# File 'lib/mocha/expectation.rb', line 60

def returns(value)
  @return_value = value
  self
end

#times(range) ⇒ Object



35
36
37
38
# File 'lib/mocha/expectation.rb', line 35

def times(range)
  @count = range
  self
end

#verifyObject



75
76
77
78
79
# File 'lib/mocha/expectation.rb', line 75

def verify
  unless (@count === @invoked) then
    raise Test::Unit::AssertionFailedError, "#{message}: expected calls: #{@count}, actual calls: #{@invoked}"
  end
end

#with(*arguments, &parameter_block) ⇒ Object



54
55
56
57
58
# File 'lib/mocha/expectation.rb', line 54

def with(*arguments, &parameter_block)
  @parameters, @parameter_block = arguments, parameter_block
  class << @parameters; def to_s; join(', '); end; end
  self
end