[rspec-users] specing nested models

Mike Mazur mmazur at gmail.com
Fri Jan 28 08:05:50 EST 2011


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


More information about the rspec-users mailing list