So I am trying to spec a nested model / form like say Post has_many Comments and in order to build a nested form I need to build a new comment in @posts.comments like so: @post = Post.new @post.comments.build Now, my question is: How do you go about correctly testing this ? Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110124/ffd480b3/attachment-0001.html>
How about: it ''builds a comment in the post'' do post = Post.new comments_assoc = mock comment = mock_model Comment post.stub(:comments).and_return comments_assoc comments_assoc.should_receive(:build).with(no_args).and_return comment # invoke something to trigger the action end -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110124/f3ae25d1/attachment.html>
That worked great, thank you. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110126/aa04fd52/attachment.html>
On Wednesday, January 26, 2011 10:34:14 AM UTC-5, Cezar Halmagean wrote:> > That worked great, thank you.You''re welcome, mate! -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110126/0be97180/attachment.html>
Hi, On Mon, Jan 24, 2011 at 19:33, Cezar Halmagean <cezar at halmagean.ro> wrote:> So I am trying to spec a nested model / form like say Post has_many Comments > and in order to build a nested form I need to build a new comment in > @posts.comments like so: > @post = Post.new > @post.comments.build > Now, my question is: How do you go about correctly testing this ?I would do something as follows: it "creates a post with a comment" do expect { post :create, :args => { :some => "args" } }.to change(Post, :count).by(1) post = Post.last post.should have(1).comments end HTH, Mike