我试着四处查看是否确实记录了let(:X)可以在before(:each)之前安全地使用,在每次“before(:each)”执行之前清除缓存值,这意味着在之前( :each)执行上一个示例中的缓存值不再使用,但在第一次调用定义的let方法时生成new on(参见下面的示例) .
从relishapp之前的(:each)/之前(:example)文档
https://relishapp.com/rspec/rspec-core/v/3-5/docs/hooks/before-and-after-hooks
“在运行示例的主体之前和/或之后,使用挂钩之前和之后执行任意代码 . ”
let()文档是relishapp:“使用let来定义一个memoized帮助器方法 . 该值将在同一个例子中的多个调用中缓存,但不会跨越示例 . ”
https://relishapp.com/rspec/rspec-core/v/3-5/docs/helper-methods/let-and-let
因此,在每个之前(:每个)/之前(:示例)执行之前,我们将获得由let而不是chached发生的“新鲜”值(这意味着根据文档,之前的那个(:每个)/之前(:示例)块被视为示例的一部分)?我知道它是有道理的,但我没有看到它在某处记录 - 有人可以提供链接到它所写的文档吗?
示例代码:
describe 'SOMETHING' do
let(:first) { Object.new }
before do
stub(first).foo? { true }
end
it 'test1' do
# will first.foo? always returns true here?
#Some test work...
end
it 'test2' do
# will first.foo? always returns true here?
# any chance the stubbing in the before occurs
# on the instance from the previous test before
# the value is cleared from the cache?
#Test2...
end
end