你会如何测试这个?我想在不同条件下多次测试一组规格

发布于 2024-12-14 04:05:17 字数 666 浏览 0 评论 0原文

我有一组使用 RSpec2 和 Capybara 编写的请求规范。下面是一个示例:

  require 'spec_helper'
  describe "Product Display and Interactions" do

  it "should hide the price and show SOLD OUT in the product listing when appropriate" do
    @product = Factory.create(:sold_out_product)
    @product.sale = @sale
    @product.save!
    visit(sale_path(@sale))
    @product.sold_out?.should eq(true)
    find("#product_#{@product.id}").should have_content('Sold Out')
  end

  [...]

  end

问题是我有几个不同的销售视图模板,每个模板都有自己的产品视图部分。是否有一种干净简单的方法来指示 RSpec 每次运行一系列具有不同条件的规范?在这种情况下,我想在 @sale 记录上设置一个属性,然后再次运行所有规范。

或者也许有更好的方法来完全测试这个场景?我是 RSpec 的新手,实际上也是 Rails 的新手。

I have a set of request specs written using RSpec2 and Capybara. Here's one example:

  require 'spec_helper'
  describe "Product Display and Interactions" do

  it "should hide the price and show SOLD OUT in the product listing when appropriate" do
    @product = Factory.create(:sold_out_product)
    @product.sale = @sale
    @product.save!
    visit(sale_path(@sale))
    @product.sold_out?.should eq(true)
    find("#product_#{@product.id}").should have_content('Sold Out')
  end

  [...]

  end

The issue is that I have several different view templates for Sale, each with their own view partials for products. Is there a clean easy way to instruct RSpec to run a series of specs with a varying condition each time? I'd like to set an attribute on the @sale record in this case and then run all the specs over again.

Or maybe there is a better approach to testing this scenario altogether? I'm new to RSpec and actually to Rails altogether.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一身软味 2024-12-21 04:05:17

有“更好”的方法来测试这一点,但是,目前,如果您是新手,我建议您习惯测试和轨道,而不要混淆问题。

您可以根据您当前的情况执行以下操作。这将为 @sale#attribute_to_alter 上的每个变体创建一个单独的示例

require 'spec_helper'
describe "Product Display and Interactions" do

    ["attr_value_1", "attr_value_2"].each do |sale_attr_value|
      it "should hide the price and show SOLD OUT in the product listing when sale attribute is set to #{sale_attr_value}" do
        @product = Factory.create(:sold_out_product)
        @sale.attribute_to_alter = sale_attr_value
        @product.sale = @sale
        @product.save!
        visit(sale_path(@sale))
        @product.sold_out?.should eq(true)
        find("#product_#{@product.id}").should have_content('Sold Out')
      end
    end

  [...]

end

There are 'better' ways to test this, but, for the time being, if you are new, I'd recommend getting used to testing and rails without confusing the issue.

You can do something like the following for your current situation. This will create a separate example for each variation on @sale#attribute_to_alter

require 'spec_helper'
describe "Product Display and Interactions" do

    ["attr_value_1", "attr_value_2"].each do |sale_attr_value|
      it "should hide the price and show SOLD OUT in the product listing when sale attribute is set to #{sale_attr_value}" do
        @product = Factory.create(:sold_out_product)
        @sale.attribute_to_alter = sale_attr_value
        @product.sale = @sale
        @product.save!
        visit(sale_path(@sale))
        @product.sold_out?.should eq(true)
        find("#product_#{@product.id}").should have_content('Sold Out')
      end
    end

  [...]

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