代码之家  ›  专栏  ›  技术社区  ›  David Hempy

删除rspec中的测试-更改(Model,:count)失败-为什么需要重新加载?

  •  7
  • David Hempy  · 技术社区  · 10 年前

    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
    

    注意最令人困惑的部分: expected #count not to have changed, but did change from 0 to 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 查看当前值?

    1 回复  |  直到 10 年前
        1
  •  10
  •   Rodrigo    10 年前

    这是因为当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 的文档:

    使用let定义一个内存化的助手方法。该值将被缓存 跨同一示例中的多个调用,但不跨示例。

    注意,let是懒求值的 :直到第一次 调用它定义的方法的时间。你可以使用let!强制 方法的调用。

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