问题描述
我的教师控制器的创建操作中有以下行.
I have the following line in my create action of my teachers controller.
@rating = @teacher.ratings.build(params[:rating]) unless params[:rating][:rating].blank?
我知道我的关联是正确的,因为除非将评分留空,否则这条线会正确地在新老师旁边创建一个新评分.但是,我正在尽最大努力遵循 TDD,但我不知道如何使用 rspec 测试该行.我有点不知所措.
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.
推荐答案
你可以这样说:
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.)
顺便说一句,你不应该测试这一行,因为它已经被 Rails 测试过了.不过,如果此行嵌入到某个操作中,您应该测试其行为.
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.
这篇关于在 Rails 3 中使用 Rspec 测试构建关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!