Issue with STI and Factory Girl Rails - ruby-on-rails

I am having an issue with my classes not being set appropriately when dealing with a Single Table Inheritance class and factory girl.
My Factory is:
factory :team do
name "Test name"
store_password 'password'
end
factory :sales_team, class: Team, parent: :team do
type 'SalesTeam'
end
factory :retail_sales_team, class: Team, parent: :team do
type 'RetailSalesTeam'
end
(I have also tried it with the :sales_team and :retail_sales_team nested inside the factory :team do)
And in my spec I do
#team = FactoryGirl.create(:retail_sales_team, name: "Test Team")
If I then call
#team.class.name #=> "Team"
And when I try to pass #team to an object that
belongs_to :team , class_name: 'RetailSalesTeam'
I get the error
Failure/Error: #sale = FactoryGirl.create(:sale, kpi: #kpi, user: #user, team: #team)
ActiveRecord::AssociationTypeMismatch:
RetailSalesTeam(#70151785667180) expected, got Team(#70151730184960)
Let me know if there is any other information I can provide and thanks in advance!

Related

How to specify the association with factory_bot?

For example I have two models a user and a post. A post belongs_to a user and a user has many posts
#spec/factories/post.rb
FactoryBot.define do
factory :post do
user
body Faker::Movie.quote
posted_at "2018-04-03 13:33:05"
end
end
#spec/factories/user.rb
FactoryBot.define do
factory :user do
first_name 'Jake'
end
end
Using Rspec in a test I want to do this:
user = create(:user, first_name: 'Barry') #id 1
post = create(:post, user: user)
I would expect that the user_id of post to be 1 however it is creating another user prior and the user_id is 2.
How can you specify the association when you are creating the object with factory_bot / factory_girl?
You should use explicit associations instead of implicit ones:
#spec/factories/post.rb
FactoryBot.define do
factory :post do
association :user # <<<--- here the change
body Faker::Movie.quote
posted_at "2018-04-03 13:33:05"
end
end
#spec/factories/user.rb
FactoryBot.define do
factory :user do
first_name 'Jake'
end
end
https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#associations
Another option is to use #trait method within the parent.
FactoryBot.define do
factory :post do
user nil
body Faker::Movie.quote
posted_at "2018-04-03 13:33:05"
end
end
FactoryBot.define do
factory :user do
first_name 'Jake'
end
trait :with_post do
after(:create) do |user|
create(:post, user_id: user.id)
end
end
end
FactoryBot.create(:user, :with_post)
Here we have another solution in case your association name and factory name is different then you can follow below syntax.
#spec/factories/post.rb
FactoryBot.define do
factory :post do
association :author, factory: :user
body Faker::Movie.quote
posted_at "2018-04-03 13:33:05"
end
end
in case your factory name is author and model name is user then you can use above syntax

Factory girl : validates associated leads to undefined method valid? for nil:Nilclass

I have model Student, which has_one :account.
All input-related data is stored inside of account. Student model just plays it's role when it comes to relations (some other models belong to student).
Problem: I can't test it with factory girl.
factory :student do
end
As I can't define anything besides it.
What I get on every attempt of #student = FactoryGirl.create(:student):
undefined method `valid?' for nil:NilClass
Any fixes?
Additional code
class Account < ActiveRecord::Base
belongs_to :account_holder, :polymorphic => true
...
end
factory :account do
sequence :name do |n|
"Name#{n}"
end
sequence :surname do |n|
"Surname#{n}"
end
sequence :phone do |n|
"8911222332#{n}"
end
sequence :email do |n|
"somemail#{n}#mail.ru"
end
student
end
Source of issue
Student has:
validates_associated_extended :account
which is basically usual validate but with error extraction for parent model.
So when FactoryGirl attempts to create student, it validates account, which is nil.
I tried this:
before(:create) {|student| build(:account, :account_holder =>student )}
in student factory, while in account factory:
association :account_holder, :factory=>:student
But it still doesn't work.
factory :student do
after :build do |student|
student.account << create(:account, :account_holder => student) if student.account_holder.nil?
end
end
This allows you to have both a valid student and to specify an account if you want to force one.
I think latest versions of FactoryGirl even allow that to be written as lazy attribute syntax:
factory :student do
account { create(:account) }
end

How to call nested factory_girl attribute

I am using rspec and factory girl and am having a weird problem getting a sub class from a factory. I am creating a designer, which is a sub cat of a user, however the test is still receiving a user, not a designer.
FactoryGirl.define do
factory :user do
factory :designer do
role: "designer"
end
end
end
describe StoreRating do
it "should have a rating" do
user = FactoryGirl.create(:designer)
store = FactoryGirl.create(:store)
StoreRating.create(designer: user, store: store, rating: 5)
end
end
Try:
FactoryGirl.define do
factory :user do
factory :designer, class: Designer do
role: "designer"
end
end
end

FactoryGirl define attribute by calling method on another factory

Here is an example from the FactoryGirl documentation:
FactoryGirl.define do
factory :post do
name "Post name"
user
end
end
In this example, user is invoking another factory. What I would like to do is effectively call user.id, but to set it as the definition of an attribute. Here's a stripped-down example:
**models/job.rb**
...
belongs_to :assignee, :class_name => "User"
belongs_to :user
...
attr_accessible :assignee_id, :user_id
...
end
**factories/jobs.rb**
FactoryGirl.define do
factory :job do
assignee_id user.id #what I would like to do, but triggers "undefined method 'id'" error
user_id user.id #user_id is an attribute of the model and is the job assignor
end
I've tried to incorporate the part of the documentation that discusses aliases, but with no luck:
FactoryGirl.define do
factory :user, :aliases => [:assignee] do
....
I feel like (hope?) I'm close here, but any insight is appreciated. Thanks.
EDIT: This code gets my specs running!
**factories/jobs.rb**
FactoryGirl.define do
factory :job do
before(:create) do |job|
user = FactoryGirl.create(:user)
job.assignee = user
job.user = user
end
association :assignee, factory: :user
association :user, factory: :user
sequence(:user_id) { |n| n }
sequence(:assignee_id) { |n| n }
...
end
And it passes my it { should be_valid } spec, so it seems that the factory is fine, though I think I have some refactoring in the spec itself when I'm calling FactoryGirl.create.
The code above incorporates the suggestions from mguymon. Thanks!
FINAL UPDATE
After going back and re-reading Hartl's discussion on model associations, I was able to put this matter to rest. What I have above was techincally valid, but didn't actually pass the attributes in properly when i built or created jobs in my spec. Here's what I should have had:
FactoryGirl.define do
factory :job do
association :assignee, factory: :user
user
end
end
My problem also stemmed from how I was creating factories in my spec, so here's how I should have been doing it (but wasn't...sigh):
let(:user) { create(:user) }
before { #job = create(:job, user: #user) }
It seems that I don't explicitly have to have association :user in my factory, nor do I need the before block from above.
As an aside, I also learned that I can debug by including puts #job within an expect statement, or call #job.assignee_id to make sure that the attributes are being loaded properly. When that particular spec is run, the puts statement will output right by the F or . from the spec.
For the latest version of FactoryGirl, use association to map to other ActiveRecord Models:
factory :job do
# ...
association :assignee, factory: :user
end
This is straight from the docs.
From the error you posted, it is stating you are trying to get the user.id but user is not an ActiveRecord instance but a proxy from FactoryGirl. You will not get this error if you are using the association method. If you need to directly access a model, you have to manually build it first. You can do this by passing a block in your factory:
factory :job do
assignee_id { FactoryGirl.create(:user).id }
end
It looks like you are trying to associate the same model twice, for this you can use before_create callback to create a User model and assign to user and assignee:
factory :job do
before(:create) do |job|
user = FactoryGirl.create(:user)
job.assignee = user
job.user = user
end
end

Rails 3 - Factory girl gem - belongs_to and has_one relation

I have User and Teacher models. Teacher belongs_to User and User has_one Teacher. Also i have the code in factory girl file:
Factory.define :user do |user|
user.user_login "Another User"
user.user_role "admin"
user.password "foobar"
end
Factory.sequence :user_login do |n|
"person-#{n}"
end
Factory.define :teacher do |teacher|
teacher.teacher_last_name 'Last'
teacher.teacher_first_name 'First'
teacher.teacher_middle_name 'Middle'
teacher.teacher_birthday '01.11.1980'
teacher.teacher_category 'First category'
teacher.teacher_sex 'm'
end
When i try to create a teacher in my spec:
#teacher = Factory(:teacher)
Then I recieve the error:
Failure/Error: #teacher = Factory(:teacher)
ActiveRecord::RecordInvalid:
Validation failed: User can't be blank
As i understand that happens because i don't tell Factory that my teacher belongs_to user. How can i fix that?
You should define association:
Factory.define :teacher do |teacher|
...
teacher.user
end
Factory Girl has wonderful tutorial, I recommend you to look at it.
P.S. Why would you want to add those strange prefixes (user_, teacher_) to model attributes? It looks very ugly, so you definitely do something wrong.

Resources