2017-01-12 27 views
0

私はCloud 9 + Ruby on Rails + Rspec + Capybara + FactoryGirl + DatabaseCleanerを使用しています。これまでは、開発用にsqliteを使用していましたが、sqliteでは動作しないチャート機能を実行していました。そこで私はPostgresqlを使って移行しました。SqliteからPostgresqlへの移行後にRspecテストが失敗する

私は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' 

私はそれが作成されたときに1のproperty_type_idがproperty_typesテーブル内の1のIDと存在していないため、このエラーがhappingていると思います。代わりに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 

は、ここにプロパティの工場:

ここでは210

は、ユーザーの工場です:

FactoryGirl.define do 

    factory :user do 
     email "[email protected]" 
     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 

私は数日間、この問題を調査してきたとしてすべてのヘルプは大歓迎されます。

+0

外部キー制約エラーがあります。 'PG :: ForeignKeyViolation:ERROR:テーブルの"プロパティ "の挿入または更新は、外部キー制約" fk_rails_680868ce5b "に違反します。 'migration 'を投稿してもらえますか – fossil

答えて

0

プロパティー・ファクトリーでは、property_type_idは常に1を参照しています。これは、property_type表で見つからない場合に外部キー・エラーを発生させます。

propertyに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 

または、idが1のproperty_typeレコードをtest dbに残しておくことができます。

+0

Zhong、ここでは最初に表示されたエラーを解決するために推奨された変更を行いましたが、今は他のエラーがあります。これまで私が行ってきたことがあります。 - 関連付けを使用するために更新されたプロパティファクトリ。 - 更新されたプロパティタイプ関連を使用するファクトリです。 - 'belongs_to:user'参照を持つ更新されたプロパティモデル。障害: 1)物件の作成変更が正常に完了した 失敗/エラー:@property = current_user.properties.find(のparams [:ID]) のActiveRecord :: RecordNotFound: は見つかりませんでしたプロパティ 'ID' と= 3 [WHERE "properties"。 "user_id" =?] – j03z

+0

どのようにプロパティレコードを作成しますか? 'current_user.properties.create'?または 'Properties.new'です。私には、プロパティレコードにuser_idがないと感じています。 –

+0

'current_user.properties.create'は' current_user'のプロパティを作成します –

関連する問題