在 Rails 3 中使用 Rspec 测试构建关联
我的教师控制器的创建操作中有以下行。
@rating = @teacher.ratings.build(params[:rating]) unless params[:rating][:rating].blank?
我知道我的关联是正确的,因为除非评级留空,否则这条线会与新老师一起正确创建新评级。然而,我正在尽力遵循 TDD,但我不知道如何使用 rspec 测试该行。我有点不知所措。
我正在使用工厂女孩,如果有帮助的话应该可以。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以这样说:(
即评级是否属于用户。)
顺便说一句,您不应该测试这一行,因为它已经由 Rails 测试过。不过,如果该行嵌入到某些操作中,您应该测试该行为。
You can say something like :
(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.