次のようなスペックファイルがあります。
# foo_spec.rb
class Foo
end
describe Foo do
let(:foo) { 'foo' }
subject { bar }
# before(:all) { foo } # The seond example fails if uncomment this line.
describe 'test one' do
let(:bar) { 'one' }
it { should == 'one' }
end
describe 'test two' do
let(:bar) { 'two' }
it { should == 'two' }
end
end
どちらの例も期待どおりに合格しています。ただし、before(:all)のコメントを外すと、2番目の例は失敗します。
1) Foo test two
Failure/Error: it { should == 'two' }
expected: "two"
got: "one" (using ==)
AFAIK、let()の値は、同じ例の複数の呼び出しにわたってキャッシュされますが、例間ではキャッシュされません。したがって、before(:all)を使用したときに2番目の例が失敗する理由はよくわかりません。
私はruby1.9.2p180とrspec2.6.4を使用しています