Forum: RSpec Testing variables in controllers?

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.
Fbd9cb107fe7c941333d6a3488691989?d=identicon&s=25 Ramon Tayag (ramontayag)
on 2008-11-06 16:21
(Received via mailing list)
How do you test that your controller fetched the right records?

I have an action that returns a different set of records based on
whether or not the current_user is the "owner" of the profile being
viewed.

Code is here http://pastie.org/308685.

"controller.submissions.size.should == 1" makes sense but it doesn't
work.  Of course, I might be doing this wrong altogether!  Very new to
rspec.

Thanks!
Ramon Tayag
059ed46172a087063ce26250e44c8627?d=identicon&s=25 Fernando Perez (fernando)
on 2008-11-06 16:39
Ramon Tayag wrote:
> How do you test that your controller fetched the right records?
>
> I have an action that returns a different set of records based on
> whether or not the current_user is the "owner" of the profile being
> viewed.
>
> Code is here http://pastie.org/308685.
>
> "controller.submissions.size.should == 1" makes sense but it doesn't
> work.  Of course, I might be doing this wrong altogether!  Very new to
> rspec.
>
> Thanks!
> Ramon Tayag

From what I read and my little experience with rpsec, you shouldn't test
for the exact return values. This kind of test should be done when
testing the model alone.
Cdf378de2284d8acf137122e541caa28?d=identicon&s=25 Matt Wynne (mattwynne)
on 2008-11-06 17:04
(Received via mailing list)
woah!

You're exposing way too much of your model here, IMO. That is forcing
you to do all that noisy mocking setup in your before block.

Can you make a method on your user object like User#valid_submissions
and have that return the correct submissions?

How do you know whether the user is the profile owner?
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick Hoffman (Guest)
on 2008-11-06 18:15
(Received via mailing list)
On 2008-11-06, at 09:36, Ramon Tayag wrote:
> rspec.
>
> Thanks!
> Ramon Tayag

Hi Ramon. I'd refactor most of what's in the "before :show do" block
into a couple of User instance methods (or whichever model @user
belongs to). For example: http://pastie.org/308814

Cheers,
Nick
Fbd9cb107fe7c941333d6a3488691989?d=identicon&s=25 Ramon Tayag (ramontayag)
on 2008-11-06 18:43
(Received via mailing list)
Cool thanks Fernando, Matt, Nick - it seems like I haven't made my
models fat enough.  That seems to be the answer :)

Ramon Tayag
This topic is locked and can not be replied to.