gpt4 book ai didi

ruby-on-rails - 删除 rspec 中的测试 - 更改(模型,:count) failing - Why is reload needed?

转载 作者:数据小太阳 更新时间:2023-10-29 07:05:35 24 4
gpt4 key购买 nike

TLDR:App.count 需要重新加载才能看到创建的记录。为什么?

我找到了很多关于测试 DELETE 方法的引用资料,如下所示:

expect { delete_request }.to change(App, :count).by(-1)

这是有道理的,并且适用于一些类似的场景。但是,我在测试不应该执行的删除时遇到了一个问题,例如当没有用户登录时。

我从这里开始,用两种方法来测试同一件事:

require 'rails_helper'

RSpec.describe V1::AppsController, type: :controller do
let(:user) { create(:user) }
let(:app) { create(:app, account: user.account) }

describe 'DELETE destroy when you are not logged in' do
let(:delete_request) { delete :destroy, id: app, format: :json }

it 'does not delete the app (.count)' do
expect { delete_request }.not_to change(App, :count)
end

it 'does not delete the app (.exists?)' do
delete_request
expect(App.exists?(app.id)).to eq(true)
end
end
end

这是 rspec 所说的:

V1::AppsController
DELETE destroy when you are not logged in
does not delete the app (.count) (FAILED - 1)
does not delete the app (.exists?)

Failures:

1) V1::AppsController DELETE destroy when you are not logged in does not delete the app (.count)
Failure/Error: expect { delete_request }.not_to change(App, :count)
expected #count not to have changed, but did change from 0 to 1
# ./spec/controllers/v1/delete_test_1.rb:11:in `block (3 levels) in <top (required)>'

2 examples, 1 failure

请注意最令人困惑的部分:预期#count 没有改变,但确实从 0 变为 1。嗯?我试图进行非法删除,但我的记录数增长 了一个?另请注意,明确检查主题记录是否仍然存在是有效的。

所以我又尝试了一些,发现我可以通过在 expect() 之前重新加载来解决问题:

it 'does not delete the app (.count)' do
puts "App.count is #{App.count} (after create(:app))"
app.reload
puts "App.count is #{App.count} (after reload)"
expect { delete_request }.not_to change(App, :count)
puts "App.count is #{App.count} (after request)"
end

现在 rspec 很高兴:

V1::AppsController
DELETE destroy when you are not logged in
App.count is 0 (after create(:app))
App.count is 1 (after reload)
App.count is 1 (after request)
does not delete the app (.count)
does not delete the app (.exists?)

2 examples, 0 failures

基于所有这些,我决定坚持使用 exists? 方法。但另一个(可能更大)问题是,我在互联网上找到的所有测试样本都用于测试创建记录,例如 expect { create_request }.to change(App, :count).by(1)可能是误报,如果他们看到与我相同的结果,并假设记录是在实际上是缓存工件时创建的?

那么,知道为什么 App.count 需要 reload 才能看到当前值吗?

最佳答案

发生这种情况是因为当 ruby​​ 调用 delete_request 方法时,它会调用 app 方法,并创建一个 App

要对此进行测试,请在调用 expect rspec 方法之前创建应用:

it 'does not delete the app (.count)' do
app #creates the app
expect { delete_request }.not_to change(App, :count)
end

看看let的文档:

Use let to define a memoized helper method. The value will be cached across multiple calls in the same example but not across examples.

Note that let is lazy-evaluated: it is not evaluated until the first time the method it defines is invoked. You can use let! to force the method's invocation before each example.

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

关于ruby-on-rails - 删除 rspec 中的测试 - 更改(模型,:count) failing - Why is reload needed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28350460/

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