为什么rspec没有记住这个?

bbc*_*cro 1 ruby rspec

我有一个测试,有点像以下.细节并不重要,但我有一个大约需要10秒的方法,并且在一堆测试中获取一些我想要多次使用的数据.数据不会再那么新鲜 - 我只需要获取一次.我对let的理解是它的memoizes,所以我希望以下内容只能调用slow_thing一次.但是当我提到慢速时,我看到它被称为多次.我究竟做错了什么?

describe 'example' do

  def slow_thing
    puts "CALLING ME!"
    sleep(100)
  end

  let(:slowthing) { slow_thing }

  it 'does something slow' do
    expect(slowthing).to be_true
  end

  it 'does another slow thing' do
    expect(slowthing).to be_true
  end

end
Run Code Online (Sandbox Code Playgroud)

当我进行测试时,我看到了打电话给我!多次,因为我有断言或使用慢.

Dav*_*ton 5

文档状态值不会跨示例缓存:

该值将在同一示例中的多个调用之间缓存,但不跨示例缓存.[强调我的.]

例如,也来自文档:

$count = 0
describe "let" do
  let(:count) { $count += 1 }

  it "memoizes the value" do
    count.should == 1
    count.should == 1
  end

  it "is not cached across examples" do
    count.should == 2
  end
end
Run Code Online (Sandbox Code Playgroud)

来自https://www.relishapp.com/rspec/rspec-core/v/2-6/docs/helper-methods/let-and-let