最適化したい低速仕様がいくつかあります。そのような仕様の例は次のようになります。
require 'rspec'
class HeavyComputation
def compute_result
sleep 1 # something compute heavy here
"very big string"
end
end
describe HeavyComputation, 'preferred style, but slow' do
subject { described_class.new.compute_result }
it { should include 'big' }
it { should match 'string' }
it { should match /very/ }
# +50 others
end
これは非常に読みやすく、一般的には満足しています。ただし、仕様を追加するたびに合計実行時間が少なくとも 1 秒長くなります。それはあまり受け入れられません。
HeavyComputation
(この質問の範囲外であるため、クラスの最適化については議論しないでください)。
したがって、私が頼らなければならないのは、次のような仕様です。
describe HeavyComputation, 'faster, but ugly' do
subject { described_class.new.compute_result }
it 'should have expected result overall' do
should include 'big'
should match 'string'
should match /very/
# +50 others
end
end
実行時間は常にほぼ一定であるため、これは明らかにパフォーマンス面ではるかに優れています。問題は、障害を追跡するのが非常に難しく、直感的に読み取ることができないことです。
理想的には、両方を混在させたいと思います。これらの行に沿ったもの:
describe HeavyComputation, 'what I want ideally' do
with_shared_setup_or_subject_or_something_similar_with do
shared(:result) { described_class.new.compute_result }
subject { result }
it { should include 'big' }
it { should match 'string' }
it { should match /very/ }
# +50 others
end
end
しかし、残念ながら、どこから実装を開始すればよいかわかりません。これには複数の潜在的な問題があります (共有結果でフックを呼び出す必要があります)。
この問題に対する既存の解決策があるかどうか知りたいこと。いいえの場合、それに取り組むための最良の方法は何ですか?