次のような仕様ファイルがいくつかあります。
describe "My DSL" do
before :each do
@object = prepare_my_object
end
describe "foo" do
before :each do
@result = @object.read_my_dsl_and_store_stuff_in_database__this_is_expensive
end
it "should do this" do
@result.should be_this
end
it "should not do that" do
@result.should_not be_that
end
# ... several more tests about the state of @result
end
# ...
end
before :each
2 番目のブロックが毎回実行されるため、これらのテストには時間がかかります。代わりに使用before :all
しても、アウターの前に呼び出されるため、実際には役に立ちませんbefore :each
。すべての期待を 1 つのit
ブロックにまとめることは役に立ちますが、これは悪いスタイルと見なされます。
高価なメソッドを 1 回だけ実行するベスト プラクティスは何ですか?