2016-10-11 82 views
1

救星。DatabaseCleaner似乎沒有清理套裝

我在每個RSpec示例後清理數據庫時遇到問題。 問題是,當我運行rspec命令時,users_controller_spec.rb會抱怨存在比示例所期望的更多的記錄。事實上,如果我使用rails c進行檢查,正在創建記錄。 當我單獨運行這個套件時,它會成功,所以我認爲這是因爲DatabaseCleaner不會清除其他規範創建的用戶記錄(用戶記錄的數量與記錄中的其他記錄user_controller_spec示例相匹配)。他們創建在before :all區塊(如果有的話)。

這是我rails_helper.rb

# 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

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記錄創建

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 
+0

您的配置看起來OK。其他規格可能存在錯誤(不在* users_controller_apec.rb *中)。你有多少個規格?嘗試按組執行規格以找出創建的記錄。 –

+0

我目前有8個控制器規格。並且額外的記錄正在前端javascript ajaxes用戶信息和api rails檢索某些用戶記錄(因此用戶記錄分解)的示例中創建。 – Hiro

+0

你可以展示這些規格嗎? (將它們添加到您的問題中) –

回答

2

不幸的RSpec的before(:all)不TRAN發揮很好傳統測試。 before(:all)中的代碼在事務打開之前運行,這意味着在事務中止時任何創建的記錄都不會回滾。您有責任在after(:all)中手動清潔這些物品。

rspec-rails#496Using before(:all) in RSpec will cause you lots of trouble unless you know what you are doing

after(:all) do 
    # before/after(:all) is not transactional; see https://www.relishapp.com/rspec/rspec-rails/docs/transactions 
    DatabaseCleaner.clean_with(:truncation) 
    end 
+0

謝謝!有效!從來沒有想過會有這個乾淨而合乎邏輯的答案。畢竟,文件規則.. – Hiro

+0

不錯,很高興它幫助! – gmcnaughton