如何使用Shoulda来测试Rails中的控制器和新的AR创建?

发布于 2024-10-08 20:20:42 字数 1176 浏览 3 评论 0原文

我刚开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

入画浅相思 2024-10-15 20:20:42

该示例更适合单元测试。尽管您在控制器中触发操作,但逻辑位于模型中。

应该向 Test::Unit 添加便利功能。诸如上下文和匹配器之类的东西。

我会这样测试:

context '.new_todos' do
  setup do
    @campaign = Campaign.create(:name => 'My Campaign')
    @email = @campaign.emails.build(:subject => 'Test Campaign Email')
    @email.save
    @email.new_todos
  end

  should 'generate todos for all contacts' do
    assert @email.todos.count > 0
  end
end

显然,示例属性需要更改,并且您需要确保获得所需的结果(我猜测并使用了 @email.todos),但它是一个开始。如果您可以尝试一下并看看会发生什么,我将很乐意更新。

要在控制器中对此进行测试,您需要进行功能或集成测试。使用shoulda 进行功能测试也非常简单。看起来像这样:

context 'POST to :create' do
  setup do
    @campaign = Campaign.create(:name => 'My Campaign')
    @email = '[email protected]' # or whatever data you're expecting
    post :create, :campaign_id => @campaign.id, :email => @email
  end

  should respond_with(:redirect)
  should redirect_to('/some/path')
end

这是一个开始。祝你好运!

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:

context '.new_todos' do
  setup do
    @campaign = Campaign.create(:name => 'My Campaign')
    @email = @campaign.emails.build(:subject => 'Test Campaign Email')
    @email.save
    @email.new_todos
  end

  should 'generate todos for all contacts' do
    assert @email.todos.count > 0
  end
end

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:

context 'POST to :create' do
  setup do
    @campaign = Campaign.create(:name => 'My Campaign')
    @email = '[email protected]' # or whatever data you're expecting
    post :create, :campaign_id => @campaign.id, :email => @email
  end

  should respond_with(:redirect)
  should redirect_to('/some/path')
end

That's a start. Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文