如何使用Shoulda来测试Rails中的控制器和新的AR创建?
我刚开始使用 Shoulda,不知道从哪里开始。
我希望能够测试的一件事是,当我为给定模型创建新记录时,控制器应该为另一个相关模型创建新记录。
我该如何在Shoulda中做这个?
这是当我为电子邮件创建新记录时发生的情况:
def create
29 @campaign = Campaign.find(params[:campaign_id])
30 @email = @campaign.emails.build(params[:email])
31 if @email.save
32 @email.new_todos # create todos across all contacts for this asset
33 flash[:notice] = "Successfully created email."
34 #redirect_to campaign_url(@campaign)
35 redirect_to :back
36 else
37 render :action => 'new'
38 end
39 end
“@email.new_todos”为模型 Todo 创建新记录,这是跨所有模型添加的扩展,例如电子邮件:
def create
29 @campaign = Campaign.find(params[:campaign_id])
30 @email = @campaign.emails.build(params[:email])
31 if @email.save
32 @email.new_todos # create todos across all contacts for this asset
33 flash[:notice] = "Successfully created email."
34 #redirect_to campaign_url(@campaign)
35 redirect_to :back
36 else
37 render :action => 'new'
38 end
39 end
我想慢慢开始合并测试,并且正在选择可能会崩溃的关键行为类型以学习如何做到这一点,
谢谢。
I am new to using Shoulda and don't know where to begin.
One of the things I want to be able to test is when I create a new record for a given Model, the controller is supposed to then create new records for another model that is related.
How do I make this in Shoulda?
Here is what happens when I create a new record for Email:
def create
29 @campaign = Campaign.find(params[:campaign_id])
30 @email = @campaign.emails.build(params[:email])
31 if @email.save
32 @email.new_todos # create todos across all contacts for this asset
33 flash[:notice] = "Successfully created email."
34 #redirect_to campaign_url(@campaign)
35 redirect_to :back
36 else
37 render :action => 'new'
38 end
39 end
'@email.new_todos" creates new records for model Todo, which is an extension added across all the models, such as Email:
def create
29 @campaign = Campaign.find(params[:campaign_id])
30 @email = @campaign.emails.build(params[:email])
31 if @email.save
32 @email.new_todos # create todos across all contacts for this asset
33 flash[:notice] = "Successfully created email."
34 #redirect_to campaign_url(@campaign)
35 redirect_to :back
36 else
37 render :action => 'new'
38 end
39 end
I would like to slowly start incorporating tests and am picking key types of behavior where it seems likely to break down to learn how to do it.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该示例更适合单元测试。尽管您在控制器中触发操作,但逻辑位于模型中。
应该向
Test::Unit
添加便利功能。诸如上下文和匹配器之类的东西。我会这样测试:
显然,示例属性需要更改,并且您需要确保获得所需的结果(我猜测并使用了
@email.todos
),但它是一个开始。如果您可以尝试一下并看看会发生什么,我将很乐意更新。要在控制器中对此进行测试,您需要进行功能或集成测试。使用shoulda 进行功能测试也非常简单。看起来像这样:
这是一个开始。祝你好运!
This example is better suited to a unit test. Although you're triggering the action in a controller, the logic is in the model.
Shoulda adds convenience features to
Test::Unit
. Things like contexts and matchers.I'd test this like so:
Obviously the sample attributes would need to change and you'll want to ensure you're getting the desired result (I guessed and used
@email.todos
), but it's a start. I'll be happy to update if you can try that out and see what happens.To test this in the controller, you'll want a functional or integration test. Functional tests are pretty easy with shoulda, too. That would look something like this:
That's a start. Good luck!