5

次のようなスペックファイルがあります。

# 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を使用しています

4

1 に答える 1

5

これはrspecの既知の問題です:

letbefore(:all)はブロックで使用するようには設計されていません

(問題に関するさらに別のチケットからrspecの貢献者myronmarstonを引用します(これは、ここで説明している動作と非常に似ているようです))。

于 2012-10-22T04:58:41.257 に答える