私は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
私は数日間、この問題を調査してきたとしてすべてのヘルプは大歓迎されます。
外部キー制約エラーがあります。 'PG :: ForeignKeyViolation:ERROR:テーブルの"プロパティ "の挿入または更新は、外部キー制約" fk_rails_680868ce5b "に違反します。 'migration 'を投稿してもらえますか – fossil