1

Rspec と Selenium を使用していくつかの自動テストをセットアップしていますが、プログラムでサンプルを作成しようとするといくつかの問題が発生します。

データの変更に基づいて複数回テストする必要がある複数の項目を含む配列があります。

配列:

def get_array
  @stuff = { thing1, thing2, thing3 }
end

テストの簡易版:

describe "My tests" do
  context "First round" do
    before { #do some stuff }

    @my_array = get_array
    @my_array.each do |k|
      it "should pass the test" do
        k.should pass_the_test
      end
    end
  end

  context "Second round" do
    before { #do some other stuff }

    @my_array = get_array
    @my_array.each do |k|
      it "should pass the test" do
        k.should pass_the_test
      end
    end
  end
end

この例ではそれほど悪くはありませんが、毎回 @my_array = get_array を呼び出さなければならないことは、明らかに DRY ではありません。より多くのテストと複雑さを追加しているので、これはすぐに手に負えなくなります。そのため、これを行うためのより簡単でより良い方法は何か疑問に思っています。

共有コンテキストなどを試してみましたが、何もうまくいかないようです。

4

1 に答える 1

1

@benny-bates さんのコメントを読んだ後、問題は before ブロックではなく、テストを呼び出す前に変数を初期化しているだけであることに気付きました。残念ながら、インスタンス変数を定数に変えるのがおそらく最善の方法のようです。

describe "My tests" do

  STUFF = {thing1, thing2, thing3}

  context "First round" do
    before { #do some stuff }

    STUFF.each do |k|
      it "should pass the test" do
        k.should pass_the_test
      end
    end
  end

  context "Second round" do
    before { #do some other stuff }

    STUFF.each do |k|
      it "should pass the test" do
        k.should pass_the_test
      end
    end
  end
end
于 2013-03-06T03:24:34.973 に答える