首页 文章

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

提问于
浏览
5

我有一个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

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

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

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

我正在使用ruby 1.9.2p180和rspec 2.6.4

1 回答

  • 5

    这是a known problem与rspec:

    let不适用于before(:all)块

    Quoting rspec贡献者myronmarston来自yet another ticket关于这个问题(这看起来与你在这里描述的行为非常相似)) .

相关问题