0

コントローラーの動作をテストしたいとしますが、GET 経由で JSON 文字列を受け入れます。

現在、テスト クラス @testJson に var がありますが、時々、これらの JSONS に予期しないことが発生します (ie 内の不正な文字)。そのため、別のテスト ケースを追加したいと思います。

しかし、別の var @problematicJson1 を追加する (そしておそらくもっとあるかもしれません) ことは良い考えではないようです。

そのような「フィクスチャ」を維持するための最良の方法は何ですか? それらをファイルに保存してロードする必要がありますか? 私が知らないフィクスチャ機能はありますか?

4

1 に答える 1

1

それらのものは据え付け品ではありません。

変数を遅延定義できるようにするRSpecのきちんとした機能を使用する必要があります(RSpecを使用している場合)。そのため、実際の変数は、外部の「コンテキストで定義されている場合でも、特定の「it」によって使用された場合にのみインスタンス化されます。 /describe」ブロック。

https://www.relishapp.com/rspec/rspec-core/v/2-6/docs/helper-methods/let-and-let

context "some context" do
  let(:testJson) { put your json inside the block }
  let(:otherJson) { {:my_json => textJson} } # this will use the defined testJson

  it "something" do
    testJson.should have_key "blah"
  end

  context "some internal context"
    let(:testJson) { something else }

    it "some other test" do
      otherJson[:my_json].should .... 
      # this will use the local version of testJson
      # you only have to redefine the things you need to, unlike a before block
    end
  end
end
于 2012-11-01T08:12:18.387 に答える