当使用before(:all)时,let()值在示例中缓存?

Dao*_*ang 5 ruby testing rspec

我有一个spec文件,看起来像:

# 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
Run Code Online (Sandbox Code Playgroud)

这两个例子都按预期传递.但是,如果我取消注释before(:all),则第二个示例将失败:

1) Foo test two
     Failure/Error: it        { should == 'two' }
       expected: "two"
            got: "one" (using ==)
Run Code Online (Sandbox Code Playgroud)

AFAIK,let()的值将在同一示例中的多个调用中缓存,但不跨示例缓存.所以当使用before(:all)时,不太确定为什么它会失败第二个例子.

我正在使用ruby 1.9.2p180和rspec 2.6.4

pje*_*pje 5

这是rspec的一个已知问题:

lets不适用于before(:all)

(引用 rspec贡献者myronmarston来自该问题的另一张票(这看起来与你在这里描述的行为非常相似)).