测试方法是否命中数据库

Test if method hits the database

我有以下方法,我想确保它不会访问数据库,除非子域发生变化。

class ApplicationController < ActionController::API
  def current_account
    unless @current_account && @current_account.subdomain == request.subdomain
      @current_account = Account.find_by subdomain: request.subdomain
    end
    @current_account
  end
end

我怎样才能测试最差的那个?

require 'rails_helper'
RSpec.describe ApplicationController, type: :controller do
  controller do
    def index
    end
  end

  describe 'current_account' do
    before(:each) do
      FactoryGirl.create(:account, subdomain: 'subdomain1')
      FactoryGirl.create(:account, subdomain: 'subdomain2')
      request.host = 'subdomain1.example.com'
    end

    it 'sets the current_account based on the subdomain' do
      get :index
      expect(subject.send(:current_account).subdomain).to eq('subdomain1')
    end

    it 'changes the current_account when subdomain changes' do
      get :index
      request.host = 'subdomain2.example.com'
      get :index
      expect(subject.send(:current_account).subdomain).to eq('subdomain2')
    end

    xit 'does not hit the database if subdomain does not change' do
      get :index
      # expect to hit the db
      get :index
      # expect to not hit the db
    end
  end
end

我试过expect(Account).to receive(:find)没有成功。

我通常为此目的安装此 gem:

https://github.com/brigade/db-query-matchers - RSpec 数据库查询匹配器

用法示例:

it 'does not make database queries' do
  expect { subject.make_no_queries }.to_not make_database_queries
end

我知道这是旧的,但我只是在寻找这个问题的答案,所以也许其他人会受益,因为我不需要 gem 我想要的东西。

答案的要点是:如果你不需要计算实际的数据库查询或命中(我没有,而且看起来 OP 没有要么),您可以只使用 spies on a partial double 检查是否正在调用 运行 数据库查询的方法。但是在 运行 你正在监视的方法之前,你总是必须先制造间谍。语法是 allow(foo).to receive(:bar)expect(foo).to have_received(:bar)

所以 OP 的 expect(Account).to receive(:find) 不起作用的原因是因为它应该是:

it 'does not hit the database if subdomain does not change' do
  # SET THE SPY
  allow(Account).to receive(:find)

  get :index
  # expect to hit the db
  expect(Account).to have_received(:find)

  get :index
  # expect to not hit the db
  expect(Account).to_not have_received(:find)
end

可以 变得更复杂并监视和计算特定的查询等等,如果你需要的话,因为你可以 expect(foo).to have_received(:bar).with(*args).twice 等等,但是如果你正在使用开箱即用的 ActiveRecord 和您的方法可以进行各种查询,我的猜测是使用 gem 来计算数据库命中比您自己进入源代码以找出要监视的内容更容易上。