Forum: RSpec Documentation about the returns method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
059ed46172a087063ce26250e44c8627?d=identicon&s=25 Fernando Perez (fernando)
on 2009-01-14 22:51
Hi,

From the railscasts website source code, in the episodes_controller_spec
I read:

  it "index action with search should search published episodes" do
    Episode.expects(:search_published).with('foo').returns(Episode.all)
    get :index, :search => 'foo'
  end

Here are my questions:
- What does the returns(Episode.all) mean?
- Is it stubbing what the search_published method returns?
- Is it useful to add the returns(Episode.all) method call here?
- Whats the difference with and_return(Episode.all)?
- Shouldn't we only test for:
Episode.expects(:search_published).with('foo')?
059ed46172a087063ce26250e44c8627?d=identicon&s=25 Fernando Perez (fernando)
on 2009-01-14 22:53
Fernando Perez wrote:
> Hi,
>
> From the railscasts website source code, in the episodes_controller_spec
> I read:
>
>   it "index action with search should search published episodes" do
>     Episode.expects(:search_published).with('foo').returns(Episode.all)
>     get :index, :search => 'foo'
>   end
>
> Here are my questions:
> - What does the returns(Episode.all) mean?
> - Is it stubbing what the search_published method returns?
> - Is it useful to add the returns(Episode.all) method call here?
> - Whats the difference with and_return(Episode.all)?
> - Shouldn't we only test for:
> Episode.expects(:search_published).with('foo')?

I also forgot to add:
What's the difference between expects and should_receive? I can get my
head around it.
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-01-15 00:17
(Received via mailing list)
On Wed, Jan 14, 2009 at 3:53 PM, Fernando Perez <lists@ruby-forum.com>
wrote:
>>
>> Here are my questions:
>> - What does the returns(Episode.all) mean?

This calls Episode.all one time and stores the result. When Episode
receives :search_publishe with 'foo', that's what it returns. Make
sense?

>> - Is it stubbing what the search_published method returns?

Yes

>> - Is it useful to add the returns(Episode.all) method call here?

If you don't, nothing will be returned. If the application code relies
on the return value, then it is definitely useful - necessary, in
fact.

>> - Whats the difference with and_return(Episode.all)?

No semantic difference - different library (see below).

>> - Shouldn't we only test for:
>> Episode.expects(:search_published).with('foo')?

No - the returns statement is not part of the expectation - it's the
value to return when search_published is received.

>
> I also forgot to add:
> What's the difference between expects and should_receive? I can get my
> head around it.

Two different mocking libraries:

spec/mocks:

  Episode.should_receive(:search_published).with('foo').and_return(Episode.all)

mocha (used in the screen cast):

  Episode.expects(:search_published).with('foo').returns(Episode.all)

HTH,
David
059ed46172a087063ce26250e44c8627?d=identicon&s=25 Fernando Perez (fernando)
on 2009-01-16 16:20
David Chelimsky wrote:
> On Wed, Jan 14, 2009 at 3:53 PM, Fernando Perez <lists@ruby-forum.com>
> wrote:
>>>
>>> Here are my questions:
>>> - What does the returns(Episode.all) mean?
>

Okay I get it now. Thank you very much.
This topic is locked and can not be replied to.