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, backtrace = nil) ⇒ Expectation

Returns a new instance of Expectation.



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

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

Instance Attribute Details

#backtraceObject (readonly)

Returns the value of attribute backtrace.



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

def backtrace
  @backtrace
end

#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



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

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

#at_least_onceObject



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

def at_least_once()
  at_least(1)
  self
end

#invoke {|@parameters_to_yield| ... } ⇒ Object

Yields:

  • (@parameters_to_yield)


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

def invoke
  @invoked += 1
  yield(*@parameters_to_yield) if yield? and block_given?
  @return_value.is_a?(Proc) ? @return_value.call : @return_value
end

#match?(method_name, *arguments) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#messageObject



96
97
98
99
100
# File 'lib/mocha/expectation.rb', line 96

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

#neverObject



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

def never
  times(0)
end

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



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

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

#returns(value) ⇒ Object



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

def returns(value)
  @return_value = value
  self
end

#times(range) ⇒ Object



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

def times(range)
  @count = range
  self
end

#verify {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



87
88
89
90
91
92
93
94
# File 'lib/mocha/expectation.rb', line 87

def verify
  yield(self) if block_given?
  unless (@count === @invoked) then
    failure = Test::Unit::AssertionFailedError.new("#{message}: expected calls: #{@count}, actual calls: #{@invoked}")
    failure.set_backtrace(backtrace)
    raise failure
  end
end

#with(*arguments, &parameter_block) ⇒ Object



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

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

#yield?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/mocha/expectation.rb', line 28

def yield?
  @yield
end

#yields(*parameters) ⇒ Object



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

def yields(*parameters)
  @yield = true
  @parameters_to_yield = parameters
  self
end