Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec Stubbing: Return the parameter

Tags:

Though my question is pretty straightforward, I failed to find an answer around here:

How can I stub a method and return the parameter itself (for example on a method that does an array-operation)?

Something like this:

 interface.stub!(:get_trace).with(<whatever_here>).and_return(<whatever_here>) 
like image 967
Len Avatar asked May 09 '11 14:05

Len


1 Answers

Note: The stub method has been deprecated. Please see this answer for the modern way to do this.


stub! can accept a block. The block receives the parameters; the return value of the block is the return value of the stub:

class Interface end  describe Interface do   it "should have a stub that returns its argument" do     interface = Interface.new     interface.stub!(:get_trace) do |arg|       arg     end     interface.get_trace(123).should eql 123   end end 
like image 108
Wayne Conrad Avatar answered Sep 22 '22 08:09

Wayne Conrad