[rspec-users] Could anyone please help with rspec/nested resource behavior checking?

Fischer, Daniel daniel at danielfischer.com
Sat Jun 2 04:31:21 EDT 2007


Jonathon,

Not sure - I don't really know what to do at this point, but I have a
new problem again lol.

Anyway, I can't seem to create new mailing messages? I guess I ask to many...

Well, forgive me - but I'll just attach it here since I can't make a new one.

Hey,

Sorry for so many questions - I'm really bad at this right now.

I'm trying to cover the following code w/ rspec

 def index
   if params[:user_id]
     @user = User.find(params[:user_id])
     @messages = @user.messages
   end
 end

So basically what I'm doing is listing all the messages for a user,
provided there is an id parameter.

describe MessagesController, " handling GET /messages for a user" do

 before do
   @message = mock_model(Message)
   @message.stub!(:user_id).and_return(1)
   @user = mock_model(User)
   @user.stub!(:id).and_return(1)
   User.stub!(:messages).and_return([@message])
   User.stub!(:find).and_return([@user])
 end

 def do_get
   get :index, :user_id => 1
 end

 it "should be successful" do
   do_get
   response.should be_success
 end

 it "should render index template" do
   do_get
   response.should render_template('index')
 end

 it "should find all messages" do
   User.should_receive(:messages).and_return([@message])
   do_get
 end

 it "should assign the found messages for the view" do
   do_get
   assigns[:messages].should == [@message]
 end
end


I'm trying to use the basic scaffold spec, but I'm absolutely clueless
on what the proper way to handle this is, I'm not even sure the proper
way I should mock the messages method, so it'll return a "stub?" of a
collection, or whatever the proper term is.

I'm sorry you have to deal with a newb, but if you could really help
me out I'd appreciate it a ton, thanks!

On 6/1/07, Jonathan Linowes <jonathan at parkerhill.com> wrote:
>
> I haven't thought it through (no pun intended) but maybe this could
> be simplified and generalized , like add a ".through" to the stub and/
> or should_receive
> ??
>
>
> On May 31, 2007, at 1:01 AM, Chris Anderson wrote:
>
> > I usually do something like this in my before(:each) section:
> >
> > User.stub!(:find).and_return(@u = mock_model(User, :posts => @pc =
> > mock('posts collection')))
> > @pc.stub(:find).and_return([@p = mock_model(Post)])
> >
> > with the corresponding should_receives in my various examples... the
> > trick is to mock the user's posts collection as its own object.
> >
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


-- 
-Daniel Fischer

http://danielfischer.com - Geek Blog
http://abigfisch.com - Portfolio
http://writersbeat.com - Writing Community


More information about the rspec-users mailing list