I'm using Cloud 9 + Ruby on Rails + Rspec + Capybara + FactoryGirl + DatabaseCleaner. Up until now, I've been using sqlite for development and ran across charting functionality that wouldn't work with sqlite. So I migrated over to using Postgresql.
Now when I run my rspec test, it fails. I can revert back to using sqlite and the test run just fine at that point.
Here is one of the errors I'm receiving:
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'
I think this error is happing because the property_type_id of 1 doesn't exists with an ID of 1 in the property_types table when it is created. Instead the ID for property_types is 2. It seems as if the ID column is not resetting back to 1 after each example.
Here's my 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
Here's the Database.yml file:
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
Here is one of the test that fail:
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
Here's the Property Factory:
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
Here's the User Factory:
FactoryGirl.define do
factory :user do
email "tester@tester.com"
first_name "Test"
last_name "Test"
password "abcd1234"
password_confirmation "abcd1234"
end
end
Here's the Property Type Factory:
FactoryGirl.define do
factory :property_type do
name "Single Family"
end
end
Any help will be greatly appreciated as I've been researching this issue for several days.
In Property Factory, the property_type_id is always referencing 1, which will raise foreign key error if it cannot be found in property_type table.
You can change it to use association if property has a relationship with property_type, and it will create property_type dynamically.
FactoryGirl.define do
factory :property do
address "111 Test Dr"
city "Test"
state "TS"
zip "999999"
property_type
user_id 1
end
end
Or you can have a property_type record with id 1 persisted in test db.