Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 为什么不是';t rspec会记住这件事吗?_Ruby_Rspec - Fatal编程技术网

Ruby 为什么不是';t rspec会记住这件事吗?

Ruby 为什么不是';t rspec会记住这件事吗?,ruby,rspec,Ruby,Rspec,我有一个测试,有点像下面的。细节并不重要,但我有一个方法,大约需要10秒,并获取一些数据,我想在一系列测试中多次使用这些数据。数据不会再新鲜了,我只需要取一次。我对let的理解是它会记忆,所以我希望下面只调用slow_thing一次。但我看到它被称为很多次,因为我提到慢行。我做错了什么 describe 'example' do def slow_thing puts "CALLING ME!" sleep(100) end let(:slowthing) { s

我有一个测试,有点像下面的。细节并不重要,但我有一个方法,大约需要10秒,并获取一些数据,我想在一系列测试中多次使用这些数据。数据不会再新鲜了,我只需要取一次。我对let的理解是它会记忆,所以我希望下面只调用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

当我运行测试时,我看到正在呼叫我!我有断言或使用slowthing的次数相同。

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

该值将缓存在同一示例中的多个调用中,但不会缓存在示例中。[我的重点。]

例如,也可从文件中获取:

$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

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

该值将缓存在同一示例中的多个调用中,但不会缓存在示例中。[我的重点。]

例如,也可从文件中获取:

$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

来自

谢谢-我靠的是记忆。。。我犯了错。@bbcmicro这是一个不同的例子,所以它没有被记忆。谢谢-我依靠的是记忆。。。这是一个错误。@bbcmicro这是一个不同的例子,所以它没有被记录。请使用(:all)之前的
而不是(:all)
之前的