2011-11-07 20 views
0

我有一套使用RSpec2和水豚書寫的請求規格。這裏有一個例子:你會如何測試?我想在不同的條件下多次測試一組規格

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。

回答

1

有更好的方法來測試這個,但是,暫時來說,如果你是新手,我會建議習慣測試和導軌而不會混淆問題。

對於您當前的情況,您可以執行以下操作。這將爲@ 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 
相關問題