My problem seems very common, but I haven't found any answer in the documentation or the internet itself.
It might seem a clone of this question has_many while respecting build strategy in factory_girl but 2,5 years after that post factory_girl changed a lot.
I have a model with a has_many relation called photos. I want to populate this has many relation preserving my choice of build strategy.
If I call offering = FactoryGirl.build_stubbed :offering, :stay I expect offering.photos to be a collection of stubbed models.
The only way i've found to achieve this is this one:
factory :offering do
association :partner, factory: :named_partner
association :destination, factory: :geolocated_destination
trait :stay do
title "Hotel Gran Vía"
description "Great hotel in a great zone with great views"
offering_type 'stay'
price 65
rooms 70
stars 4
event_spaces 3
photos do
case #build_strategy
when FactoryGirl::Strategy::Create then [FactoryGirl.create(:hotel_photo)]
when FactoryGirl::Strategy::Build then [FactoryGirl.build(:hotel_photo)]
when FactoryGirl::Strategy::Stub then [FactoryGirl.build_stubbed(:hotel_photo)]
end
end
end
end
No need to say that IT MUST EXIST a better way of do that.
Ideas?
You can use the various FactoryGirl callbacks:
factory :offering do
association :partner, factory: :named_partner
association :destination, factory: :geolocated_destination
trait :stay do
title "Hotel Gran Vía"
description "Great hotel in a great zone with great views"
offering_type 'stay'
price 65
rooms 70
stars 4
event_spaces 3
after(:stub) do |offering|
offering.photos = [build_stubbed(:hotel_photo)]
end
after(:build) do |offering|
offering.photos = [build(:hotel_photo)]
end
after(:create) do |offering|
offering.photos = [create(:hotel_photo)]
end
end
end
Here's a slightly cleaner version of Flipstone's answer:
factory :offering do
trait :stay do
...
photos do
association :hotel_photo, :strategy => #build_strategy.class
end
end
end
You can also invoke the FactoryRunner class directly and pass it the build strategy to use.
factory :offering do
trait :stay do
...
photos do
FactoryGirl::FactoryRunner.new(:hotel_photo, #build_strategy.class, []).run
end
end
end
Other answers have a flaw, the inverse association is not being properly initialized, e.g. offering.photos.first.offering == offering is false. Even worse that being incorrect, the offering is a new Offering for each of the photos.
Also, explicitly specifying a strategy is redundant.
To overcome the flow and to simplify things:
factory :offering do
trait :stay do
...
photos do
association :hotel_photo, offering: #instance
end
end
end
#instance is an instance of the Offering being created by the factory at the moment. For the curious, context is FactoryGirl::Evaluator.
If you don't like the #instance like I do, you may look in evaluator.rb and find the following:
def method_missing(method_name, *args, &block)
if #instance.respond_to?(method_name)
#instance.send(method_name, *args, &block)
I really like how itself looks:
factory :offering do
trait :stay do
...
photos do
association :hotel_photo, offering: itself
end
end
end
Do be able to use itself, undefine it on the Evaluator:
FactoryGirl::Evaluator.class_eval { undef_method :itself }
It will be passed to the #instance and will return the #instance itself.
For the sake of providing a full example with several photos:
factory :offering do
trait :stay do
...
photos do
3.times.map do
association :hotel_photo, offering: itself
end
end
end
end
Usage:
offering = FactoryGirl.build_stubbed :offering, :stay
offering.photos.length # => 3
offering.photos.all? { |photo| photo.offering == offering } # => true
Be careful, some things might not work as expected:
offering.photos.first.offering_id will surprisingly be nil;
offering.photos.count will hit the database with a SELECT COUNT(*) FROM hotel_photos ... (and will return 0 in most cases), please use length or size in assertions.
This kind of thing works for me:
factory :offering do
trait :stay do
...
photos { |o| [o.association(:hotel_photo)] }
end
end
Related
I have a FactoryGirl for a model class. In this model, I defined some traits. In some traits, I don't want FactoryGirl callback calling but I don't know how. For example here is my code:
FactoryGirl.define do
factory :product do
sequence(:promotion_item_code) { |n| "promotion_item_code#{n}" }
after :create do |product|
FactoryGirl.create_list :product_details, 1, :product => product
end
trait :special_product do
# do some thing
# and don't want to run FactoryGirl callback
end
end
In this code, I don't want :special_product trait calls after :create. I don't know how to do this.
#Edit: the reason I want to this because sometimes I want generate data from parent -> children. But sometimes I want vice versa generate from children to parent. So When I go from children -> parent, callback at parent is called so children is created twice. That is not what I want.
#Edit 2: My question is prevent callback from FactoryGirl, not from ActiveRecord model.
Thanks
You can use transient attributes to achieve that.
Like:
factory :product do
transient do
create_products true
end
sequence(:promotion_item_code) { |n| "promotion_item_code#{n}" }
after :create do |product, evaluator|
FactoryGirl.create_list(:product_details, 1, :product => product) if evaluator.create_products
end
trait :special_product do
# do some thing
# and don't want to run FactoryGirl callback
end
end
But I think that a better way to model this problem is to define a trait for the "base case" or to have multiple factories.
You could use the same approach as described in the Factory Girl docs for a has_many relationship:
factory :product_detail do
product
#... other product_detail attributes
end
factory :product do
sequence(:promotion_item_code) { |n| "promotion_item_code#{n}" }
factory :product_with_details do
transient do
details_count 1 # to match your example.
end
after(:create) do |product, evaluator|
create_list(:product_detail, evaluator.details_count, product: product)
end
end
trait :special_product do
# do some thing
# and don't want to run FactoryGirl callback
end
end
This allows you to generate data for the parent->children:
create(:product_with_details) # creates a product with one detail.
create(:product_with_details, details_count: 5) # if you want more than 1 detail.
...and for the special product just
# does not create any product_details.
create(:product)
create(:product, :special_product)
To generate for children->parent
create(:product_detail)
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 i can create object with association
I have Post model and PostsPhoto
PostsPhoto => belongs_to :post
Post => has_many :post_photos, dependent: :destroy,class_name: 'PostsPhoto'
I have tried
FactoryGirl.define do
factory :post do
article Faker::Lorem.paragraph(4)
video 'http://youtube.com'
author Faker::Name.name
category 'article'
title Faker::Name.title
post_photos
end
end
and have got
NoMethodError: undefined method `each' for #<PostsPhoto:0x007ff68e3bd698>
i cant use after(:create) {},because i validate it on create
def check_slider_photo
errors.add(:post_photos, 'Add post photos') if self.post_photos.size <= 0
end
I want to create(:post)=> returns me post object with post_photos
There are a couple ways around this. It depends on what you're testing exactly, but if you don't need to alter the objects or use them in queries, try using build_stubbed instead of create.
That is, if you'd typically say, FactoryGirl.create(:post), try instead saying FactoryGirl.build_stubbed(:post).
Another option would be not to run your validation in test and test that validation separately. To do this you can say
post = FactoryGirl.build(:post)
post.save(validate: false)
Then create the other objects and pass them post.id Happy testing.
Try this:
FactoryGirl.define do
factory :post do
article Faker::Lorem.paragraph(4)
video 'http://youtube.com'
author Faker::Name.name
category 'article'
title Faker::Name.title
factory :post_with_photos do
ignore do
photos_count 3
end
after(:build) do |post, evaluator|
post.post_photos << build_list(:post_photo, evaluator.photos_count)
end
end
end
end
I can't seem to get FactoryGirl to call my before_add callback with an associated model.
I've got a Course model with:
has_many :lessons, dependent: :destroy, before_add: :set_lesson_number
def set_lesson_number
#stuff
end
a Lesson model with:
belongs_to :course.
a Lesson factory with:
FactoryGirl.define do
factory :lesson do
course
end
end
and a Course factory, defined as suggested by the Factory Girl wiki:
FactoryGirl.define do
factory :course do
factory :course_with_lessons do
transient do
lessons_count 10
end
after(:create) do |course, evaluator|
create_list(:lesson, evaluator.lessons_count, course: course)
end
end
end
end
The before_add callback doesn't get called when I do FactoryGirl.create(:lesson), but it does get called if I do this:
lesson = FactoryGirl.build(:lesson)
course = lesson.course
course.lessons << l
In both cases, course.lessons ends up including lesson.
By the same token, FactoryGirl.build(:course_with_lessons) doesn't work with the above Course factory, but if I replace the create_list line with:
evaluator.lessons_count.times do
course.lessons << build(lesson)
end
it does. It seems like FactoryGirl is creating the Lessons and setting their Course ID's, but somehow not actually "adding" them to the collection, so I have to do it manually.
Am I missing something about how FactoryGirl is supposed to work? Or about how ActiveRecord works?
This is how ActiveRecord works.
If you run the following in your rails console you'll see the before_add callback on the association is not called:
course = Course.create
Lesson.create(course_id: course.id)
I imagine FactoryGirl.create_list generates objects in a similar way.
The lesson needs to be added to the collection in order for the callback to fire. This can be done in a couple of ways.
1. Create a Lesson through course
course.lessons.create
2. Explicitly add the lesson to course.lessons
course.lessons << lesson
3. Explicitly add a collection of lesson to course.lessons
course.lessons = [lesson1, lesson2]
To get the callback to fire, you could modify your factory like so:
factory :course do
factory :course_with_lessons do
transient do
lessons_count 10
end
after(:create) do |course, evaluator|
course.lessons =
build_list(:lesson, evaluator.lessons_count, course: course)
course.save
end
end
end
Hope that helps.
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.'