gpt4 book ai didi

ruby - 为什么 rspec 不记住这个?

转载 作者:太空宇宙 更新时间:2023-11-03 17:34:25 24 4
gpt4 key购买 nike

我有一个测试,有点像下面这样。细节并不重要,但我有一个方法需要大约 10 秒,并取回一些我想在一堆测试中多次使用的数据。数据不会再新鲜了——我只需要获取一次。我对 let 的理解是它会内存,所以我希望下面的代码只调用 slow_thing 一次。但我看到它被调用的次数与我提到的 slowthing 一样多。我做错了什么?

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

当我运行测试时,我看到 CALLING ME!与我有断言或使用 slowthing 的次数一样多。

最佳答案

文档声明值跨示例缓存:

The value will be cached across multiple calls in the same example but not across examples. [Emphasis mine.]

例如,同样来自文档:

$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

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

关于ruby - 为什么 rspec 不记住这个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21855731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com