Class: RSpec::Mocks::MessageExpectation

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/mocks/message_expectation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argument_list_matcher=(value) ⇒ Object (writeonly)

Sets the attribute argument_list_matcher

Parameters:

  • value

    the value to set the attribute argument_list_matcher to.



8
9
10
# File 'lib/rspec/mocks/message_expectation.rb', line 8

def argument_list_matcher=(value)
  @argument_list_matcher = value
end

#messageObject (readonly)

Returns the value of attribute message.



7
8
9
# File 'lib/rspec/mocks/message_expectation.rb', line 7

def message
  @message
end

Instance Method Details

#and_raiseObject #and_raise(ExceptionClass) ⇒ Object #and_raise(exception_instance) ⇒ Object

Note:

When you pass an exception class, the MessageExpectation will raise an instance of it, creating it with new. If the exception class initializer requires any parameters, you must pass in an instance and not the class.

Tells the object to raise an exception when the message is received.

Examples:


car.stub(:go).and_raise
car.stub(:go).and_raise(OutOfGas)
car.stub(:go).and_raise(OutOfGas.new(2, :oz))


118
119
120
# File 'lib/rspec/mocks/message_expectation.rb', line 118

def and_raise(exception=RuntimeError)
  @exception_to_raise = exception
end

#and_return(value) ⇒ Object #and_return(first_value, second_value) ⇒ Object #and_return(&block) ⇒ Object

Tells the object to return a value when it receives the message. Given more than one value, the first value is returned the first time the message is received, the second value is returned the next time, etc, etc.

If the message is received more times than there are values, the last value is received for every subsequent call.

The block format is still supported, but is unofficially deprecated in favor of just passing a block to the stub method.

Examples:


counter.stub(:count).and_return(1)
counter.count # => 1
counter.count # => 1

counter.stub(:count).and_return(1,2,3)
counter.count # => 1
counter.count # => 2
counter.count # => 3
counter.count # => 3
counter.count # => 3
# etc

# Supported, but ...
counter.stub(:count).and_return { 1 }
counter.count # => 1

# ... this is prefered
counter.stub(:count) { 1 }
counter.count # => 1


94
95
96
97
98
# File 'lib/rspec/mocks/message_expectation.rb', line 94

def and_return(*values, &implementation)
  @expected_received_count = [@expected_received_count, values.size].max unless ignoring_args? || (@expected_received_count == 0 and @at_least)
  @consecutive = true if values.size > 1
  @implementation = implementation || build_implementation(values)
end

#and_throw(symbol) ⇒ Object #and_throw(symbol, object) ⇒ Object

Tells the object to throw a symbol (with the object if that form is used) when the message is received.

Examples:


car.stub(:go).and_throw(:out_of_gas)
car.stub(:go).and_throw(:out_of_gas, :level => 0.1)


132
133
134
# File 'lib/rspec/mocks/message_expectation.rb', line 132

def and_throw(symbol, object = nil)
  @args_to_throw = [symbol, object].compact
end

#and_yield(*args) {|@eval_context = Object.new.extend(RSpec::Mocks::InstanceExec)| ... } ⇒ Object

Tells the object to yield one or more args to a block when the message is received.

Examples:


stream.stub(:open).and_yield(StringIO.new)

Yields:

  • (@eval_context = Object.new.extend(RSpec::Mocks::InstanceExec))


142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rspec/mocks/message_expectation.rb', line 142

def and_yield(*args, &block)
  if @args_to_yield_were_cloned
    @args_to_yield.clear
    @args_to_yield_were_cloned = false
  end

  yield @eval_context = Object.new.extend(RSpec::Mocks::InstanceExec) if block

  @args_to_yield << args
  self
end

#any_number_of_times(&block) ⇒ Object

Allows an expected message to be received any number of times.



349
350
351
352
353
# File 'lib/rspec/mocks/message_expectation.rb', line 349

def any_number_of_times(&block)
  @implementation = block if block
  @expected_received_count = :any
  self
end

#at_least(n, &block) ⇒ Object

Constrain a message expectation to be received at least a specific number of times.

Examples:


dealer.should_recieve(:deal_card).at_least(9).times


317
318
319
320
321
# File 'lib/rspec/mocks/message_expectation.rb', line 317

def at_least(n, &block)
  @implementation = block if block
  set_expected_received_count :at_least, n
  self
end

#at_most(n, &block) ⇒ Object

Constrain a message expectation to be received at most a specific number of times.

Examples:


dealer.should_recieve(:deal_card).at_most(10).times


329
330
331
332
333
# File 'lib/rspec/mocks/message_expectation.rb', line 329

def at_most(n, &block)
  @implementation = block if block
  set_expected_received_count :at_most, n
  self
end

#exactly(n, &block) ⇒ Object

Constrain a message expectation to be received a specific number of times.

Examples:


dealer.should_recieve(:deal_card).exactly(10).times


305
306
307
308
309
# File 'lib/rspec/mocks/message_expectation.rb', line 305

def exactly(n, &block)
  @implementation = block if block
  set_expected_received_count :exactly, n
  self
end

#neverObject

Expect a message not to be received at all.

Examples:


car.should_receive(:stop).never


360
361
362
363
# File 'lib/rspec/mocks/message_expectation.rb', line 360

def never
  @expected_received_count = 0
  self
end

#once(&block) ⇒ Object

Expect a message to be received exactly one time.

Examples:


car.should_receive(:go).once


370
371
372
373
374
# File 'lib/rspec/mocks/message_expectation.rb', line 370

def once(&block)
  @implementation = block if block
  set_expected_received_count :exactly, 1
  self
end

#ordered(&block) ⇒ Object

Expect messages to be received in a specific order.

Examples:


api.should_receive(:prepare).ordered
api.should_receive(:run).ordered
api.should_receive(:finish).ordered


394
395
396
397
398
399
# File 'lib/rspec/mocks/message_expectation.rb', line 394

def ordered(&block)
  @implementation = block if block
  @order_group.register(self)
  @ordered = true
  self
end

#raise_out_of_order_errorObject



265
266
267
# File 'lib/rspec/mocks/message_expectation.rb', line 265

def raise_out_of_order_error
  @error_generator.raise_out_of_order_error @message
end

#times(&block) ⇒ Object

Syntactic sugar for exactly, at_least and at_most

Examples:


dealer.should_recieve(:deal_card).exactly(10).times
dealer.should_recieve(:deal_card).at_least(10).times
dealer.should_recieve(:deal_card).at_most(10).times


342
343
344
345
# File 'lib/rspec/mocks/message_expectation.rb', line 342

def times(&block)
  @implementation = block if block
  self
end

#twice(&block) ⇒ Object

Expect a message to be received exactly two times.

Examples:


car.should_receive(:go).twice


381
382
383
384
385
# File 'lib/rspec/mocks/message_expectation.rb', line 381

def twice(&block)
  @implementation = block if block
  set_expected_received_count :exactly, 2
  self
end

#with(*args, &block) ⇒ Object

Constrains a stub or message expectation to invocations with specific arguments.

With a stub, if the message might be received with other args as well, you should stub a default value first, and then stub or mock the same message using with to constrain to specific arguments.

A message expectation will fail if the message is received with different arguments.

Examples:


cart.stub(:add) { :failure }
cart.stub(:add).with(Book.new(:isbn => 1934356379)) { :success }
cart.add(Book.new(:isbn => 1234567890))
# => :failure
cart.add(Book.new(:isbn => 1934356379))
# => :success

cart.should_receive(:add).with(Book.new(:isbn => 1934356379)) { :success }
cart.add(Book.new(:isbn => 1234567890))
# => failed expectation
cart.add(Book.new(:isbn => 1934356379))
# => passes


293
294
295
296
297
# File 'lib/rspec/mocks/message_expectation.rb', line 293

def with(*args, &block)
  @implementation = block if block_given? unless args.empty?
  @argument_list_matcher = ArgumentListMatcher.new(*args, &block)
  self
end