首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >DatabaseCleaner似乎在两套西装之间都不干净

DatabaseCleaner似乎在两套西装之间都不干净
EN

Stack Overflow用户
提问于 2016-10-11 08:11:49
回答 1查看 673关注 0票数 1

救世主。

在每个RSpec示例之后,我在清理数据库方面遇到了麻烦。问题是,当我运行rspec命令时,users_controller_spec.rb会抱怨记录比示例所期望的要多。事实上,如果我检查rails c,记录就会按照它的规定创建。当我单独运行这个套件时,它将是成功的,所以我认为这是因为DatabaseCleaner没有清理其他规范创建的用户记录(用户记录的数量与users_controller_spec示例声称的额外记录相匹配)。它们是在before :all块中创建的(如果这重要的话)。

这是我的rails_helper.rb

代码语言:javascript
运行
复制
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'

# Add additional requires below this line. Rails is not loaded until this point!
require 'devise'
require 'admin/v1/dashboard_controller'
# Requires supporting ruby files with custom matchers and macros, etc, in
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  config.include Devise::Test::ControllerHelpers, type: :controller
  config.include ControllerMacros, type: :controller
  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  config.include FactoryGirl::Syntax::Methods

  config.infer_spec_type_from_file_location!

  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end
end

users_controller.rb

代码语言:javascript
运行
复制
describe 'GET #index' do
  it 'populates an array of users' do
    user1 = create(:user)
    user2 = create(:user)
    get :index
    expect(assigns(:users)).to match_array([user1, user2])
  end
  it 'renders :index template' do
    get :index, {}
    expect(response).to render_template :index
  end
end

UPDATE1:这是创建额外user记录的地方

代码语言:javascript
运行
复制
    require 'rails_helper'

describe Admin::V1::MessagesController do
  let(:admin_user) do
    admin_user = double('admin_user')
    allow(request.env['warden']).to receive(:authenticate!).and_return(admin_user)
    allow(controller).to receive(:current_admin_v1_admin_user).and_return(admin_user)
    p '==='
  end
  before { login_admin_user admin_user }

  describe 'GET #index' do
    it 'renders :index template' do
      get :index, {}
      expect(response).to render_template :index
    end
  end

  describe 'GET #get_users' do
    before :all do
      @user1 = create(:user, nickname: 'hiro')
      @user2 = create(:user, nickname: 'elise')
    end
    context 'with params' do
      it 'populates an array of users matching on nickname' do
        get :get_users, format: :json, query: 'h'
        expect(assigns(:users)).to match_array([@user1])
      end
    end
    context 'without params' do
      it 'populates an array of all users' do
        get :get_users, format: :json
        expect(assigns(:users)).to match_array([@user1, @user2])
      end
    end
  end

  describe 'GET #get_messages' do
    before :all do
      @user1 = create(:user)
      @user2 = create(:user)
      @message1 = create(:message, user_id: @user1.id)
      @message2 = create(:message, user_id: @user1.id)
      @message3 = create(:message, user_id: @user2.id)
    end
    context 'with user_id' do
      it 'populates an array of messages with the user_id' do
        get :get_messages, format: :json, user_id: @user1.id
        expect(assigns(:messages)).to match_array([@message1, @message2])
      end
    end
  end
end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-11 09:42:17

不幸的是,RSpec的before(:all)不能很好地处理事务性测试。before(:all)中的代码在事务打开之前运行,这意味着当事务中止时,在那里创建的任何记录都不会回滚。您负责手动清除after(:all)中的这些项目。

rspec-rails#496在RSpec中使用are (:all)会给您带来很多麻烦,除非您知道自己在做什么

代码语言:javascript
运行
复制
  after(:all) do
    # before/after(:all) is not transactional; see https://www.relishapp.com/rspec/rspec-rails/docs/transactions
    DatabaseCleaner.clean_with(:truncation)
  end
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39972969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档