[rspec-users] should_receive and multiple return values

Ashley Moran work at ashleymoran.me.uk
Mon Dec 18 07:04:05 EST 2006


I just got bit by a misunderstanding about and_return.

I've got this controller method parse_parameters that loops over  
query string parameters and parses out vehicle registration dates.   
We have to turn these into one of three bands (Jan-Feb, Mar-Aug, Sep- 
Dec) so I wrote a method (registration_date_to_band_year_and_month)  
takes a date and returns two return values, the year and month of the  

I thought this would work:

   specify "should turn the provided registration date into a CAP  
band" do
(:registration_date_to_band_year_and_month).with(Date.new(2003, 9,  
30)).and_return(2003, 9)
(:registration_date_to_band_year_and_month).with(Date.new(2005, 3,  
1)).and_return(2005, 3)
         { :capid26149 => "rd2003-09-30,mi40000,ft15", :capid30125 =>  
"rd2005-03-01,mi73825,ft42" }

But it doesn't because and_return is setting up multiple method  
calls, not multiple return values.

I don't imagine multiple return values are used that often, but they  
can be handy.  Would there be any use in another method, maybe  
and_return_values or something to allow you to specify them?  Since  
they are possible in Ruby, it makes sense that you should be able to  
specify them in RSpec.

I got around it, of course, using an array return and the * expand  
operator, but I avoid that when I can


More information about the rspec-users mailing list