gpt4 book ai didi

ruby-on-rails - 类已泄漏到另一个示例中,不能再在规范中使用

转载 作者:行者123 更新时间:2023-12-05 04:31:42 26 4
gpt4 key购买 nike

我无法在本地复制它,但出于某种原因,我在 CircleCi 中运行测试时遇到以下错误:

<Double Mylogger> was originally created in one example but has leaked into another example and can no longer be used. rspec-mocks' doubles are designed to only last for one example, and you need to create a new one in each example you wish to use it for.

这是我的代码的简化版本:

# frozen_string_literal: true
describe 'my_rake_task' do
let(:my_log) { Mylogger.new }

subject { Rake::Task['my_rake_task'].execute }

describe 'one' do
context 'logs' do
let(:logs) do
[
['My message one'],
['My message two'],
]
end

after { subject }

it 'correctly' do
logs.each { |log| expect(my_log).to receive(:info).with(*log) }
end
end
end

describe 'two' do
context 'logs' do
let(:logs) do
[
['My message three'],
['My message four'],
]
end

after { subject }

it 'correctly' do
logs.each { |log| expect(my_log).to receive(:info).with(*log) }
end
end
end
end

为什么说 MyLogger 是双重的?为什么会漏水?

最佳答案

错误说 MyLogger 是 double 的原因是因为它是一个。当您调用 expect(my_log).to receiveallow(my_log).to receive 时,您将实例转换为 partial-double。 .

至于为什么 my_log 泄漏:从您发布的代码中无法判断。为了造成泄漏,您的 rake 任务或规范本身中的某些代码需要将 my_log 注入(inject)某个全局状态,like a class variable .

关于ruby-on-rails - 类已泄漏到另一个示例中,不能再在规范中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71803693/

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