2012-01-06 2 views
8

私は様々な入力を持ついくつかのテストを作成しています。私は、新規および返品のユーザータイプ、異なる製品、プロモーションコード、支払いオプションを使用して購買ウェブサイトをテストしています。私はこれがデータ駆動のテストセットで、おそらくテスト入力のCSV形式またはスプレッドシート形式を要求しているように感じました。因子の組み合わせを持つRSpecのテストを構成する最良の方法

私が作成した最後のテストセットに最適なrspecを使用しています。

私は一貫した結果フォーマットをしたいと思います。私はRSpecでデータテーブルを使用する方法に固執しています。 RSpecにテスト入力のテーブルを使用した人はいますか?

直接的な解決策または健全なアドバイスをお寄せいただきありがとうございます。

答えて

14

あなたがテーブルを使用するつもりなら、私はテストファイル内にインラインでそれを定義しますように...

[ 
    %w(abc 123 def ), 
    %w(wxyz 9876 ab ), 
    %w(mn 10 pqrs) 
].each do |a,b,c| 
    describe "Given inputs #{a} and #{b}" do 
    it "returns #{c}" do 
     Something.whatever(a,b).should == c 
    end 
    end 
end 
+0

ほとんど私は「それは「やるべきでテーブルを尽くすことを除いて探していますものです:私たちは、この仕様を実行すると

RSpec.describe Car do describe "#price" do it_should_behave_like "msrp", 4, "Blue", "Cloth", X it_should_behave_like "msrp", 2, "Red", "Leather", Y end end 

、出力は次の形式でなければなりませんどのような "do"部分。ありがとう! –

2
user_types = ['rich', 'poor'] 
products = ['apples', 'bananas'] 
promo_codes = [123, 234] 
results = [12,23,34,45,56,67,78,89].to_enum 
test_combis = user_types.product(products, promo_codes) 

test_combis.each do |ut, p, pc| 
    puts "testing #{ut}, #{p} and #{pc} should == #{results.next}" 
end 

出力何か:

testing rich, apples and 123 should == 12 
testing rich, apples and 234 should == 23 
testing rich, bananas and 123 should == 34 
testing rich, bananas and 234 should == 45 
testing poor, apples and 123 should == 56 
testing poor, apples and 234 should == 67 
testing poor, bananas and 123 should == 78 
testing poor, bananas and 234 should == 89 
2

ワン慣用的なアプローチは、RSpec shared examplesをパラメータとともに使用することです。私は各テーブルの行が別個のテストケースに対応していると仮定し、列は関係する変数を分解します。

例として、設定に基づいて自動車の価格を計算するコードがあるとします。クラスCarがあり、priceメソッドが製造元の推奨小売価格(MSRP)に準拠していることをテストしたいとします。

私たちは、以下の組み合わせをテストするために必要とされることがあります。

 
Doors | Color | Interior | MSRP 
-------------------------------- 
4  | Blue | Cloth | $X 
2  | Red | Leather | $Y 

はのは、この情報と正しい動作のためのテストをキャプチャし、共有の例を作成してみましょう。

RSpec.shared_examples "msrp" do |doors, color, interior, msrp| 
    context "with #{doors} doors, #{color}, #{interior}" do 
    subject { Car.new(doors, color, interior).price } 
    it { should eq(msrp) } 
    end 
end 

は、この共有の例を書いたので、私たちはその後、簡潔にコードの重複を負担することなく、複数の構成をテストすることができます。

 
Car 
    #price 
    it should behave like msrp 
     when 4 doors, Blue, Cloth 
     should equal X 
     when 2 doors, Red, Leather 
     should equal Y 
関連する問題