FactoryGirl association duplicate key error - ruby-on-rails

What is the correct way to assign associations that already exist?
I am trying to assign a has_one relationship between a user and a city, where the same city can be used by many users or other entities (e.g. an event).
Code
FactoryGirl.define do
factory :user do
name 'john'
trait :in_boston do
association :city, factory: :boston
end
end
end
Error
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "city_pkey" because it's trying to create Boston twice in the database.
What I would like to do is simply reference the existing factory, not create a new one.
My current working (but less than ideal) solution
FactoryGirl.define do
factory :user do
name 'john'
trait :in_boston do
after(:create) do |user|
user.city = City.find_by_name('Boston') || create(:boston)
end
end
end
end
Any guidance would be appreciated, thanks.

So, I'm going to assume that your model code is golden, and show you how I'd setup the test. I'm not sure why you need the factory to have decision making powers based on if the city exists or not. Just instantiate the city in its own factory and call the association in your test setup.
Factories
# factories/cities.rb
FactoryGirl.define do
factory :city do
name 'Boston'
end
end
# factories/users.rb
FactoryGirl.define do
factory :user do
name 'john'
city
end
end
Test
describe 'blah' do
let( :city ){ create :city }
let( :user ){ create :user, city: city }
it 'user should have a city' do
expect( user.city.name ).to eq 'Boston'
end
end

I had the same issue when testing a model that belonged to another model, when a callback was creating that association.
To explain simply, let's say I have a Book model, and a Page model, with Page belongs_to Book, and a callback to create Page when a book is created.
In my factory for Page, I try to associate to Book, but by doing so I create book once, and the creation of the page itself create the same book again. By UniqueIndex condition, PostgreSQL fails.
The simplest solution in that case is to not to create Page when testing the Page model, but instead to simply create(:book) and then use book.page.

Related

Can you explicitly enter a method's return value in FactoryGirl?

Assume I have the following Rails models and the method shown is tested.
class Employee < ActiveRecord::Base
has_many :jobs
def total_annual_income
jobs.collect { |j| j.annual_salary}.sum
# Or some other AR magic to do it directly in the database; doesn't matter
end
end
class Job < ActiveRecord::Base
# property :annual_salary
belongs_to :employee
end
Now, assume that I'm going to write some other method elsewhere that calls Employee#total_annual_income. When I test this method with FactoryGirl, is it possible to set up my Employee factories directly with a total_annual_income property without having to make corresponding Job factories? I.e., can I simply do
FactoryGirl.define do
factory :employee1, class: Employee do
id 100
total_annual_income 100000.0
end
end
instead of
FactoryGirl.define do
factory :employee1, class: Employee do
id 100
end
end
# WANT TO OMIT THIS ENTIRE SET OF FACTORIES #
FactoryGirl.define do
factory :employee1_job1, class: Job do
id 100
employee_id 100
annual_salary 60000.0
end
factory :employee1_job2, class: Job do
id 101
employee_id 100
annual_salary 40000.0
end
end
# WANT TO OMIT THIS ENTIRE SET OF FACTORIES #
I'm kinda new to FactoryGirl still, so apologies if I've overlooked something basic.
Have a look at the Associations information under the Factory Girl documentation:
https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#associations
This has an example of :user_with_posts which uses #create_list to generate a list of posts for a user - a bit like your list of jobs. Since on StackOverflow it is customary to include full answers in case an external link should become broken, here's copypasta of the example with its comments:
Generating data for a has_many relationship is a bit more involved, depending on the amount of flexibility desired, but here's a surefire example of generating associated data.
FactoryGirl.define do
# post factory with a `belongs_to` association for the user
factory :post do
title "Through the Looking Glass"
user
end
# user factory without associated posts
factory :user do
name "John Doe"
# user_with_posts will create post data after the user has been created
factory :user_with_posts do
# posts_count is declared as a transient attribute and available in
# attributes on the factory, as well as the callback via the evaluator
transient do
posts_count 5
end
# the after(:create) yields two values; the user instance itself and the
# evaluator, which stores all values from the factory, including transient
# attributes; `create_list`'s second argument is the number of records
# to create and we make sure the user is associated properly to the post
after(:create) do |user, evaluator|
create_list(:post, evaluator.posts_count, user: user)
end
end
end
end
This allows us to do:
create(:user).posts.length # 0
create(:user_with_posts).posts.length # 5
create(:user_with_posts, posts_count: 15).posts.length # 15
The core of this is really that #create_list method shown above.
[EDIT] Completely untested, I think your example becomes something like:
FactoryGirl.define do
factory :employee_with_jobs, class: Employee do
id 100
transient do
jobs_count 2
end
after(:create) do |employee, evaluator|
create_list(:job, evaluator.jobs_count,
employee: employee,
annual_salary: 40000.0)
end
end
end
create(:employee_with_jobs, jobs_count: 5) # Expecting total salary 200000.0.
...more or less.

How to pass argument for FactoryGirl method

There is the following code for creating objects:
FactoryGirl.define do
factory :user do
name 'Some name'
phone '89277777777'
address 'Some address'
end
factory :order do
title 'Some title'
end
end
User model has got has_many :orders association, and is it possible to pass user factory to order factory?
Thanks in advance.
Yes, as follows:
FactoryGirl.define do
factory :user do
name 'Some name'
phone '89277777777'
address 'Some address'
end
factory :order do
user
title 'Some title'
end
end
In your specs:
FactoryGirl.create(:order)
will create both an order object and an associated user object.
more information on FactoryGirl with Rails - https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md
Yes, the solution is as simple as using the association method
factory :order do
title 'Some title'
association :user
end
that can be shortened to
factory :order do
title 'Some title'
user
end
A word of warning. This approach tends to make your application very slow as long as the complexity of your models will increase.
In fact, if you have a purchase associated to an order associated to an user, etc, then creating a Purchase will create on cascade all the associated records even if you don't need them. Your tests will become slower over the time.
I suggest you to use the traits to compose your associations only when required.
factory :order do
title 'Some title'
trait :with_user do
user
end
end
FactoryGirl.create(:order, :with_user)
I normally also use :with_fake_user when I need the user id to be present for validation, but I'm testing something that doesn't require to access the user.
factory :order do
title 'Some title'
trait :with_user do
user
end
trait :with_fake_user do
user_id 0
end
end
FactoryGirl.create(:order, :with_user)
FactoryGirl.create(:order, :with_fake_user)

Avoid duplicating records in loop

I have two factories for my Company and Employee models respectively. Employee is in a belongs_to relationship with Company. Here's my two factories:
factory :company do
name "foo company"
end
factory :employee do
company
name 'Willy Bytes'
end
There are some occasions where I need to traverse an array of data and populate new Employee records accordingly to test against different conditions in my specs. To illustrate, I'm using the following specs to test one of my inclusion validations:
it "should be valid if they like red blue or green" do
["red","blue","green"].each do |c|
FactoryGirl.build(:employee, :favourite_colour => c).should be_valid
end
end
it { FactoryGirl.build(:upload, :favourite_colour => "other").should_not be_valid }
However, I have a uniqueness constraint on the parent companies name field which yields an error when I attempt to build the record. Is there an intelligent way to resolve/avoid this problem? I run into these types of specs a lot and typically what I would do is define a single Company factory and assign that to each record in the loop, but it doesn't feel intuitive and results in a lot of repetition. Is a sequence the only other way around this?
You can use factory_girl's sequence generator. Your factory will look like this.
factory :company do
sequence(:name) {|n| "company #{n}"}
end
One line:
sequence(:name) { |n| "Company #{n}" }
I found exactly what I was looking for here: Find or create record through factory_girl association
Overriding the default build / new behaviour using the initialize_with method seems to be the way to go:
factory :company do
initialize_with { Company.find_or_create_by_name("Foo Company") }
end

FactoryGirl association model trouble: "SystemStackError: stack level too deep"

I am using Ruby on Rails 3.0.9, RSpec-rails 2 and FactoryGirl. I am trying to state a Factory association model but I am in trouble.
I have a factories/user.rb file like the following:
FactoryGirl.define do
factory :user, :class => User do
attribute_1
attribute_2
...
association :account, :factory => :users_account, :method => :build, :email => 'foo#bar.com'
end
end
and a factories/users/account.rb file like the following:
FactoryGirl.define do
factory :users_account, :class => Users::Account do
sequence(:email) {|n| "foo#{n}#bar.com" }
...
end
end
The above example works as expected in my spec files, but if in the factory :users_account statement I add the association :user code so to have
FactoryGirl.define do
factory :users_account, :class => Users::Account do
sequence(:email) {|n| "foo#{n}#bar.com" }
...
association :user
end
end
I get the following error:
Failure/Error: Unable to find matching line from backtrace
SystemStackError:
stack level too deep
How can I solve that problem so to access associated models from both sides\factories (that is, in my spec files I would like to use RoR association model methods like user.account and account.user)?
P.S.: I read the Factory Girl and has_one question and my case is very close to the case explained in the linked question. That is, I have an has_one association too (between User and Users::Account classes).
According to the docs, you can't just put both sides of the associations into the factories. You'll need to use their after callback to set an object(s) to return.
For instance, in the factories/users/account.rb file, you put something like
after(:build) do |user_account, evaluator|
user_account.user = FactoryGirl.build(:user, :account=>user_account)
end
For has_many associations, you'll need to use their *_list functions.
after(:build) do |user_account, evaluator|
user_account.users = FactoryGirl.build_list(:user, 5, :account=>user_account)
end
Note: I believe the example in the docs is a bit misleading it doesn't assign anything to the object. I believe it should be something like (note the assignment).
# the after(:create) yields two values; the user instance itself and the
# evaluator, which stores all values from the factory, including ignored
# attributes; `create_list`'s second argument is the number of records
# to create and we make sure the user is associated properly to the post
after(:create) do |user, evaluator|
user.posts = FactoryGirl.create_list(:post, evaluator.posts_count, user: user)
end
Spyle's excellent answer (still working with Rails 5.2 and RSpec 3.8) will work for most associations. I had a use case where a factory needed to use 2 different factories (or different traits) for a single has_many association (ie. for a scope type method).
What I ended up coming up with was:
# To build user with posts of category == 'Special' and category == 'Regular'
after(:create) do |user, evaluator|
array = []
array.push(FactoryBot.create_list(:post, 1, category: 'Regular')
array.push(FactoryBot.create_list(:post, 1, category: 'Special')
user.posts = array.flatten
end
This allowed the user to have 1 post of category 'Regular' and 1 post of category 'Special.'

How do I test that my table design implements my associations properly?

I have written my basic models and defined their associations as well as the migrations to create the associated tables.
EDIT - Adding emphasis to what I specifically want to test.
I want to be able to test:
The associations are configured as intended
The table structures support the associations properly
I've written FG factories for all of my models in anticipation of having a complete set of test data but I can't grasp how to write a spec to test both belongs_to and has_many associations.
For example, given an Organization that has_many Users I want to be able to test that my sample Organization has a reference to my sample User.
Organization_Factory.rb:
Factory.define :boardofrec, :class => 'Organization' do |o|
o.name 'Board of Recreation'
o.address '115 Main Street'
o.city 'Smallville'
o.state 'New Jersey'
o.zip '01929'
end
Factory.define :boardofrec_with_users, :parent => :boardofrec do |o|
o.after_create do |org|
org.users = [Factory.create(:johnny, :organization => org)]
end
end
User_Factory.rb:
Factory.define :johnny, :class => 'User' do |u|
u.name 'Johnny B. Badd'
u.email 'jbadd#gmail.com'
u.password 'password'
u.org_admin true
u.site_admin false
u.association :organization, :factory => :boardofrec
end
Organization_spec.rb:
...
it "should have the user Johnny B. Badd" do
boardofrec_with_users = Factory.create(:boardofrec_with_users)
boardofrec_with_users.users.should include(Factory.create(:johnny))
end
...
This example fails because the Organization.users list and the comparison User :johnny are separate instances of the same Factory.
I realize this doesn't follow the BDD ideas behind what these plugins (FG, rspec) seemed to be geared for but seeing as this is my first rails application I'm uncomfortable moving forward without knowing that I've configured my associations and table structures properly.
Your user factory already creates an organization by virtue of the Factory Girl association method:
it "should associate a user with an organization" do
user = Factory.create(:johnny)
user.organization.name.should == 'Board of Recreation'
organization = user.organization
organization.users.count.should == 1
end
Take a look at 'log/test.log' after running your spec -- you should see an INSERT for both the organization and the user.
If you wanted to test this without the Factory Girl association, make a factory that just creates the user and make the association in the spec:
it "should associate a user with an organization" do
user = Factory.create(:johnny_no_org)
org = Factory.create(:boardofrec)
org.users.should be_empty
org.users << user
org.users.should include(user)
end
Of course all this is doing is testing whether ActiveRecord is doing its job. Since ActiveRecord is already thoroughly tested, you'll want to concentrate on testing the functionality of your application, once you've convinced yourself that the framework actually does what it's supposed to do.

Resources