gpt4 book ai didi

ruby-on-rails - Rspec:在之前的测试后保存记录

转载 作者:行者123 更新时间:2023-11-28 20:50:37 26 4
gpt4 key购买 nike

我想使用循环动态运行测试。我有什么:

let!(:product) { create(:product) }
let!(:data) { product.some_data_object }
let(:run_action) { patch :next_status, params: { id: data.id } }

STATUSES.each_with_index do |status, index|
it "changes status from #{status} to #{STATUSES[index + 1]}" do
run_action
expect { data.reload }
.to change { data.status }
.from(status)
.to(STATUSES[index + 1])
end
end
end

每个测试都应该检查 data.status 是否从数组中一步改变。但是在每次测试之前,data.status 都有数组中的第一个值。

那么如何才能得到上次测试后所有变化的旧记录呢?谢谢。

最佳答案

尝试在测试之间保留数据会导致很多问题。你最好在一个测试中运行它:

it "should change status to the next one" do
STATUSES.each_with_index do |status, index|
patch :next_status, params: { id: data.id }
expect { data.reload }
.to change { data.status }
.from(status)
.to(STATUSES[index + 1])
end
end

关于ruby-on-rails - Rspec:在之前的测试后保存记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48772034/

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