method

stub!

rspec latest stable - Class: Spec::Mocks::Methods
stub!(sym_or_hash, opts={}, &block)
public

No documentation available.

# File lib/spec/mocks/methods.rb, line 12
      def stub!(sym_or_hash, opts={}, &block)
        if Hash === sym_or_hash
          sym_or_hash.each {|method, value| stub!(method).and_return value }
        else
          __mock_proxy.add_stub(caller(1)[0], sym_or_hash.to_sym, opts, &block)
        end
      end

3Notes

stub/stub! will always be followed by '.and_return'

anexiole · Aug 14, 20111 thank

this function will aways be followed by '.and_return(...)' because a stub is typically used for returning values. The required argument given to stub is a method name. When a message to this stubbed method name is received by a class or existing object of the class AND '.and_return' is provided, the stub will return whatever was provided as argument to '.and_return'.

For example,

HomeLoan.stub!(interest_rate).and_return('5.5%')

  • this will return 5.5% when a message for interest_rate in a HomeLoan class's object is received.

HomeLoan.stub!(interest_rate).and_return('5.5%', '3%')

  • this will return 5.5% when a message for interest_rate in a HomeLoan class's object is received FOR THE FIRST TIME but will return 3% for subsequent calls/messages.

Typical stub! usage

mutru · Jun 1, 2009

Typically you would call

my_object.stub!(:updated_at).and_return(time_object)

A stub with argument and return value

emime · Jun 12, 2009

==== it "should use a dummy method with argument and return value" do dummy = mock("dummy").stub!(:emulate) dummy.should_receive(:emulate).with(:something).and_return("Done! sir!") dummy.emulate(:something).should == "Done! sir!" end