2016-12-02 69 views
1

你好傢伙我試圖建立database_cleaner,但我不能這樣做,我不知道我錯過了什麼。不能正常工作database_cleaner與Rspec測試

我有什麼到現在:

users_controller_spec

require 'rails_helper' 

describe UsersController, :type => :controller do 

    #user = User.create!(email: "[email protected]", password: "123456") 

    user = FactoryGirl.create(:user) 

    describe 'GET #show' do 

    context 'User is logged in' do 
     before do 
     sign_in user 
     end 

     it 'loads correct user details' do 
     get :show, id: user.id 
     expect(response).to have_http_status(200) 
     expect(assigns(:user)).to eq user 
     end 
    end 

    context 'No user is logged in' do 
    it 'redirects to login' do 
     get :show, id: user.id 
     expect(response).to redirect_to(new_user_session_path) 
    end 
    end 

end 
end 

支持/ database_cleaner.rb

RSpec.configure do |config| 

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

    config.before(:each) do 
    DatabaseCleaner.strategy = :transaction 
    end 

    config.before(:each, :js => true) do 
    DatabaseCleaner.strategy = :truncation 
    end 

    config.before(:each) do 
    DatabaseCleaner.start 
    end 

    config.after(:each) do 
    DatabaseCleaner.clean 
    end 

end 

rails_helper.rb有行

config.use_transactional_fixtures = false 

和註釋掉線

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } 

終端登錄

/var/lib/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/validations.rb:78:in `raise_validation_error': Validation failed: Email has already been taken (ActiveRecord::RecordInvalid) 
    from /var/lib/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/validations.rb:50:in `save!' 
    from /var/lib/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/attribute_methods/dirty.rb:30:in `save!' 
    from /var/lib/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/transactions.rb:324:in `block in save!' 
    from /var/lib/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/transactions.rb:395:in `block in with_transaction_returning_status' 
    from /var/lib/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction' 
    from /var/lib/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction' 
    from /var/lib/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction' 
    from /var/lib/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/transactions.rb:211:in `transaction' 
    from /var/lib/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/transactions.rb:392:in `with_transaction_returning_status' 
    from /var/lib/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/transactions.rb:324:in `save!' 
    from /var/lib/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/suppressor.rb:45:in `save!' 
    from /var/lib/gems/2.3.0/gems/factory_girl-4.7.0/lib/factory_girl/configuration.rb:18:in `block in initialize' 
    from /var/lib/gems/2.3.0/gems/factory_girl-4.7.0/lib/factory_girl/evaluation.rb:15:in `create' 
    from /var/lib/gems/2.3.0/gems/factory_girl-4.7.0/lib/factory_girl/strategy/create.rb:12:in `block in result' 
    from /var/lib/gems/2.3.0/gems/factory_girl-4.7.0/lib/factory_girl/strategy/create.rb:9:in `tap' 
    from /var/lib/gems/2.3.0/gems/factory_girl-4.7.0/lib/factory_girl/strategy/create.rb:9:in `result' 
    from /var/lib/gems/2.3.0/gems/factory_girl-4.7.0/lib/factory_girl/factory.rb:42:in `run' 
    from /var/lib/gems/2.3.0/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:29:in `block in run' 
    from /var/lib/gems/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/notifications.rb:166:in `instrument' 
    from /var/lib/gems/2.3.0/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:28:in `run' 
    from /var/lib/gems/2.3.0/gems/factory_girl-4.7.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method' 
    from /home/kristis/Desktop/myapp/spec/controllers/users_controller_spec.rb:7:in `block in <top (required)>' 
    from /var/lib/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/example_group.rb:385:in `module_exec' 
    from /var/lib/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/example_group.rb:385:in `subclass' 
    from /var/lib/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/example_group.rb:258:in `block in define_example_group_method' 
    from /var/lib/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/dsl.rb:43:in `block in expose_example_group_alias' 
    from /var/lib/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/dsl.rb:84:in `block (2 levels) in expose_example_group_alias_globally' 
    from /home/kristis/Desktop/myapp/spec/controllers/users_controller_spec.rb:3:in `<top (required)>' 
    from /var/lib/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `load' 
    from /var/lib/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `block in load_spec_files' 
    from /var/lib/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `each' 
    from /var/lib/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `load_spec_files' 
    from /var/lib/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:100:in `setup' 
    from /var/lib/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:86:in `run' 
    from /var/lib/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:71:in `run' 
    from /var/lib/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:45:in `invoke' 
    from /var/lib/gems/2.3.0/gems/rspec-core-3.5.4/exe/rspec:4:in `<top (required)>' 
    from /usr/local/bin/rspec:23:in `load' 
    from /usr/local/bin/rspec:23:in `<main>' 

感謝您的幫助!

+0

是您的測試數據庫乾淨你開始試運行之前? – Codebeef

回答

1

嘗試重寫此行使用let

user = FactoryGirl.create(:user) 

成爲

let(:user) { FactoryGirl.create(:user) } 
+0

它的工作!你能解釋爲什麼嗎? – Kristis

+0

它是在database_cleaners運行之外創建的,所以它一直存在。 – Codebeef

+0

let是懶惰評估的,所以現在當用戶在每次測試中首次調用時創建用戶,而不是在第一次加載測試時創建。 – Codebeef