开发者

Testing a build association using Rspec in Rails 3

I have the following line in my create action of my teachers controller.

 @rating = @teacher.ratings.build(params[:rating]) unless params[:rating][:rating].blank?

I know my associations are 开发者_开发技巧correct because this line correctly creates a new rating alongside a new teacher unless the rating is left blank. However I'm trying my best to follow TDD and I have no clue as to how to test that line using rspec. I'm kind of at a loss.

I'm using factory girl and shoulda if that helps.


You can say something like :

teacher = Factory(:teacher)
rating = Factory(:rating, :teacher_id => teacher.id)
#your_other_actions_here
teacher.rating.should be present

(That is if a rating belongs to user.)

Btw, you should not test this line, because it's already tested by Rails. You should test the behavior if this line is embedded to some action, though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜