加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – let()值在使用before(:all)时跨越示例缓存?

发布时间:2020-12-17 03:15:02 所属栏目:百科 来源:网络整理
导读:我有一个spec文件,看起来像: # foo_spec.rbclass Fooenddescribe 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 == 'on
我有一个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

解决方法

这是 a known problem与rspec:

lets are not designed to be used with before(:all) blocks

(Quoting rspec贡献者myronmarston从yet another ticket开始就问题(这看起来与你在这里描述的行为非常相似)).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读