2011-11-07 3 views
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にはまったく新しいものです。

答えて

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 
関連する問題