Testing a build association using Rspec in Rails 3

北慕城南 提交于 2020-01-05 06:02:50

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/6084346/testing-a-build-association-using-rspec-in-rails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!