不使用Rspec测试的database_cleaner

时间:2016-12-02 14:43:38

标签: ruby-on-rails ruby rspec database-cleaner

大家好我想尝试设置database_cleaner,但我无法做到这一点,我也不知道自己错过了什么。

我现在所拥有的:

users_controller_spec

require 'rails_helper'

describe UsersController, :type => :controller do

  #user = User.create!(email: "test@test.com", 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 个答案:

没有答案