How do I setup model associations in an RSpec test?

Posted by Eric M. on Stack Overflow See other posts from Stack Overflow or by Eric M.
Published on 2009-08-03T05:23:59Z Indexed on 2011/03/18 16:10 UTC
Read the original article Hit count: 158

Filed under:
|
|

I've pastied the specs I've written for the posts/show.html.erb view in an application I'm writing as a means to learn RSpec. I am still learning about mocks and stubbing. This question is specific to the "should list all related comments" spec.

What I want is to test that the show view displays a post's comments. But what I'm not sure about is how to setup this test and then have the test iterate through with should contain('xyz') statements. Any hints? Other suggestions are also appreciated! Thanks.

---Edit

Some more information. I have a named_scope applied to comments in my view (I know, I did this a bit backwards in this case), so @post.comments.approved_is(true). The code pastied responds with the error "undefined method `approved_is' for #", which makes sense since I told it stub comments and return a comment. I'm still not sure, however, how to chain the stubs so that @post.comments.approved_is(true) will return an array of comments.

© Stack Overflow or respective owner

Related posts about model

Related posts about rspec