从Sqlite迁移到Postgresql后,Rspec测试失败

时间:2017-01-12 00:20:20

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

我正在使用Cloud 9 + Ruby on Rails + Rspec + Capybara + FactoryGirl + DatabaseCleaner。到目前为止,我一直在使用sqlite进行开发,并且遇到了与sqlite不兼容的图表功能。所以我迁移到使用Postgresql。

现在,当我运行我的rspec测试时,它失败了。我可以恢复使用sqlite,测试运行就好了。

以下是我收到的错误之一:

Failures:

  2) Property creation has been completed successfully
     Failure/Error: @property = FactoryGirl.create(:property)

     ActiveRecord::InvalidForeignKey:
       PG::ForeignKeyViolation: ERROR:  insert or update on table "properties" violates foreign key constraint "fk_rails_680868ce5b"
       DETAIL:  Key (property_type_id)=(1) is not present in table "property_types".
       : INSERT INTO "properties" ("address", "city", "state", "zip", "property_type_id", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"
     # /usr/local/rvm/gems/ruby-2.3.0/gems/factory_girl-4.8.0/lib/factory_girl/configuration.rb:18:in `block in initialize'
     # /usr/local/rvm/gems/ruby-2.3.0/gems/factory_girl-4.8.0/lib/factory_girl/evaluation.rb:15:in `create'
     # /usr/local/rvm/gems/ruby-2.3.0/gems/factory_girl-4.8.0/lib/factory_girl/strategy/create.rb:12:in `block in result'
     # /usr/local/rvm/gems/ruby-2.3.0/gems/factory_girl-4.8.0/lib/factory_girl/strategy/create.rb:9:in `tap'
     # /usr/local/rvm/gems/ruby-2.3.0/gems/factory_girl-4.8.0/lib/factory_girl/strategy/create.rb:9:in `result'
     # /usr/local/rvm/gems/ruby-2.3.0/gems/factory_girl-4.8.0/lib/factory_girl/factory.rb:42:in `run'
     # /usr/local/rvm/gems/ruby-2.3.0/gems/factory_girl-4.8.0/lib/factory_girl/factory_runner.rb:29:in `block in run'
     # /usr/local/rvm/gems/ruby-2.3.0/gems/factory_girl-4.8.0/lib/factory_girl/factory_runner.rb:28:in `run'
     # /usr/local/rvm/gems/ruby-2.3.0/gems/factory_girl-4.8.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
     # ./spec/features/property_spec.rb:10:in `block (3 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # PG::ForeignKeyViolation:
     #   ERROR:  insert or update on table "properties" violates foreign key constraint "fk_rails_680868ce5b"
     #   DETAIL:  Key (property_type_id)=(1) is not present in table "property_types".
     #   /usr/local/rvm/gems/ruby-2.3.0/gems/factory_girl-4.8.0/lib/factory_girl/configuration.rb:18:in `block in initialize'

我认为这个错误很糟糕,因为在创建Property_types表时,property_type_id为1并不存在,ID为1。而不是property_types的ID是2.看起来每个示例后ID列都没有重置为1。

这是我的rails_helper.rb:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)

abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'capybara/rails'

include Warden::Test::Helpers
Warden.test_mode!


ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = false
  config.before(:suite) { DatabaseCleaner.clean_with(:truncation) }
  config.before(:each) { DatabaseCleaner.strategy = :transaction }
  config.before(:each, :js => true) { DatabaseCleaner.strategy = :truncation }
  config.before(:each) { DatabaseCleaner.start }
  config.after(:each) { DatabaseCleaner.clean }
  config.infer_spec_type_from_file_location!
  config.filter_rails_from_backtrace!
  config.include FactoryGirl::Syntax::Methods
end

这是Database.yml文件:

default: &default
      adapter: postgresql
      encoding: unicode
      pool: 5
      username: ubuntu
      password: xxxxxxxx
      template: template0

development:
  <<: *default
  database: app_development

test:
  <<: *default
  database: app_test

production:
  <<: *default
  database: app_production

这是失败的测试之一:

require 'rails_helper'

describe 'Property' do

    describe 'creation' do

        it "has been completed successfully" do            
            @user = FactoryGirl.create(:user)            
            login_as(@user, :scope => :user)  
            FactoryGirl.create(:property_type)
            @property = FactoryGirl.create(:property)
            visit new_property_path
            fill_in "property_address", with: @property.address
            fill_in "property_city", with: @property.city
            fill_in "property_state", with: @property.state
            fill_in "property_zip", with: @property.zip
            fill_in "property_units", with: @property.units  
            click_button "Save"  
            expect(@property.reload.address).to eq(@property.address)
            expect(@property.reload.city).to eq(@property.city)
            expect(@property.reload.state).to eq(@property.state)
            expect(@property.reload.zip).to eq(@property.zip)
            expect(@property.reload.units).to eq(@property.units)
    end
end

这是物业工厂:

FactoryGirl.define do

   factory :property do
      address "111 Test Dr" 
      city "Test"
      state "TS"
      zip "999999"
      property_type_id 1
      user_id 1
   end
end

这是用户工厂:

FactoryGirl.define do

   factory :user do
        email "tester@tester.com"
        first_name "Test"
        last_name "Test"
        password "abcd1234"
        password_confirmation "abcd1234"       
   end
end

这是物业类型工厂:

FactoryGirl.define do

  factory :property_type do
    name "Single Family"
  end
end

我将非常感谢任何帮助,因为我已经研究了这个问题好几天了。

1 个答案:

答案 0 :(得分:0)

在Property Factory中,property_type_id始终引用1,如果在property_type表中找不到,则会引发外键错误。

如果属性与property_type有关系,您可以将其更改为使用关联,并且它将动态创建property_type。

FactoryGirl.define do

   factory :property do
      address "111 Test Dr" 
      city "Test"
      state "TS"
      zip "999999"
      property_type
      user_id 1
   end
end

或者你可以在test db中保留一个id为1的property_type记录。