Factorybot - How to set Nested attributes - ruby-on-rails

I beleive it's better to create a new question... It follows my previous question my model product has many sizes (nested attributes)
I want to create Factories but I can't make it work...
A product is valid if it has at least one size (size_nameand quantity)
FactoryBot.define do
factory :product do
title { Faker::Artist.name}
ref { Faker::Number.number(10)}
price { Faker::Number.number(2) }
color { Faker::Color.color_name }
brand { Faker::TvShows::BreakingBad }
description { Faker::Lorem.sentence(3) }
attachments { [
File.open(File.join(Rails.root,"app/assets/images/seeds/image.jpg")),
] }
user { User.first || association(:user, admin: true)}
category { Category.first }
# SOLUTION 1
factory :size do
transient do
size_name {["S", "M", "L", "XL"].sample}
quantity { Faker::Number.number(2) }
end
end
# SOLUTION 2
after(:create) do |product|
create(:size, product: product)
end
# SOLUTION 3
initialize_with { attributes }
# Failure/Error: #product = create(:product, category_id: category.id)
# NoMethodError:
# undefined method `save!' for #<Hash:0x007ff12f0d9378>
end
end
In the controller spec
before(:each) do
sign_in FactoryBot.create(:user, admin: true)
category = create(:category)
#product = create(:product, category_id: category.id)
end
I don't know how to write the size attribute, my produt is still not valid (missing the size)
The error I get is validation failed,Product must exist...

You have to define a factory for size
FactoryBot.define do
factory :size do
size_name { ["S", "M", "L", "XL"].sample }
quantity { Faker::Number.number(2) }
end
end
and the product
FactoryBot.define do
factory :product do
association :size
title { Faker::Artist.name}
...
end
end
or add the build callback in the :product factory
after :build do |product|
product.sizes << create(:size)
end

Create a factory for sizes
FactoryBot.define do
factory :size do
size_name {["S", "M", "L", "XL"].sample}
quantity { Faker::Number.number(2) }
product
end
end
and one for products
FactoryBot.define do
factory :product do
title { Faker::Artist.name}
ref { Faker::Number.number(10)}
price { Faker::Number.number(2) }
color { Faker::Color.color_name }
brand { Faker::TvShows::BreakingBad }
description { Faker::Lorem.sentence(3) }
attachments { [
File.open(File.join(Rails.root,"app/assets/images/seeds/image.jpg")),
] }
user { User.first || association(:user, admin: true)}
category
end
end

Related

Create FactoryBot User with has_one relationship

Background:
I am trying to create a FactoryBot object which is related with has_one/belongs_to
User has_one Car
Car has_one Style
Style has an attribute {style_number:"1234"}
Question
My controller references user, user has_one Car, Car has_one Style, and I need to set these values within FactoryBot.
How do I create a User, who also has a Car object, that has a Style object?
I read the documentation https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md
However, I am not understanding how they recommend doing this. Figured out, I need to nest the three objects, but confused on the syntax.
Controller
before_action :authenticate_user!
before_action :set_steps
before_action :setup_wizard
include Wicked::Wizard
def show
#user = current_user
#form_object = form_object_model_for_step(step).new(#user)
render_wizard
end
private
def set_steps
if style_is_1234
self.steps = car_steps.insert(1, :style_car)
else
self.steps = car_steps
end
end
def style_is_1234
if params.dig(:form_object, :style_number)
(params.dig(:form_object, :style_number) & ["1234"]).present?
else
(current_user.try(:car).try(:style).try(:style_number) & ["1234"]).present?
end
end
def car_steps
[:type,:wheel, :brand]
end
Rspec Test
Factory :User
FactoryBot.define do
factory :user, class: User do
first_name { "John" }
last_name { "Doe" }
email { Faker::Internet.email }
password { "somepassword" }
password_confirmation { "some password"}
end
end
Before method
before(:each) do
#request.env["devise.mapping"] = Devise.mappings[:user]
user = FactoryBot.create(:user)
sign_in user
Test
User needs to be signed in and User.car.style.style_number needs to be set to "1234"
context "Requesting with second step CarStyle" do
it "should return success" do
get :show, params: { :id => 'car_style' }
expect(response.status).to eq 200
end
end
Currently this test fails because User.Car.Style.style_number is not set to "1234".
Trial 1 (https://github.com/thoughtbot/factory_bot_rails/issues/232)
FactoryBot.define do
factory :user, class: User do
first_name { "John" }
last_name { "Doe" }
email { Faker::Internet.email }
password { "somepassword" }
password_confirmation { "some password"}
car
end
end
FactoryBot.define do
factory :car, class: Car do
make { "Holden" }
model { "UTE" }
end
end
FactoryBot.define do
factory :style, class: Style do
color { "blue" }
for_car
trait :for_car do
association(:styable, factory: :car)
end
end
end
Error from trail 1
SystemStackError:
stack level too deep
Trail 2
I tried srng's recommendation
EDIT: For a polymorphic association try;
FactoryBot.define do
factory :car, class: Car do
make { "Holden" }
model { "UTE" }
association :stylable, factory: :style
end
end
and got error:
ActiveRecord::RecordInvalid: Validation failed: Stylable must exist
I think this is a rails 5 issue. https://github.com/rails/rails/issues/24518
However, I would like to keep my code with the adding the optional:true. Any way to do this?
Trail 3
FactoryBot.define do
factory :car, class: Car do
make { "Holden" }
model { "UTE" }
after(:create) do |car|
create(:style, stylable: car)
end
end
end
Tried Srng's second recommendation and although it worked for him, I got a slightly different error:
ActiveRecord::RecordInvalid:
Validation failed: User must exist
In order to create dependent Factories you have to create a factory for each model, and then just add the dependent Model name to your factory, ie.
FactoryBot.define do
factory :user, class: User do
first_name { "John" }
last_name { "Doe" }
email { Faker::Internet.email }
password { "somepassword" }
password_confirmation { "some password"}
car
end
end
FactoryBot.define do
factory :car, class: Car do
make { "Holden" }
model { "UTE" }
style
end
end
FactoryBot.define do
factory :style, class: Style do
color { "blue" }
end
end
EDIT:
Relevant code;
# Factories
FactoryBot.define do
factory :user, class: User do
first_name { "John" }
last_name { "Doe" }
email { Faker::Internet.email }
password { "somepassword" }
password_confirmation { "some password"}
after(:create) do |user|
user.car ||= create(:car, :user => user)
end
end
end
factory :style, class: Style do
style_number { "Blue" }
end
factory :car, class: Car do
name { "Holden" }
trait :style do
association :stylable, factory: :style
end
end
#models
class Car < ApplicationRecord
has_one :style, as: :styleable
end
class Style < ApplicationRecord
belongs_to :styleable, polymorphic: true
belongs_to :car
end
# Migrations - The belongs_to is the only important one
class CreateStyles < ActiveRecord::Migration[5.2]
def change
create_table :styles do |t|
t.string :style_number
t.belongs_to :stylable, polymorphic: true
t.timestamps
end
end
end
class CreateCars < ActiveRecord::Migration[5.2]
def change
create_table :cars do |t|
t.string :name
t.timestamps
end
end
end
There can be an another way of attaining this using a transient block in the Factory.
Hope the below snippet may help you to explore in new way.
Note: This is not tested.
## To Create a user in test case
# create(:user) # defaults to 1234 style number
# create(:user, car_style_number: 5678)
DEFAULT_STYLE_NUMBER = 1234
FactoryBot.define do
factory :user do
transient do
car_style_number { DEFAULT_STYLE_NUMBER }
end
first_name { "John" }
last_name { "Doe" }
email { Faker::Internet.email }
after(:create) do |user, evaluator|
user.car = create(:car, car_style_number: evaluator.car_style_number, user: user)
end
end
end
FactoryBot.define do
factory :car do
transient do
car_style_number { DEFAULT_STYLE_NUMBER }
end
make { "Holden" }
model { "UTE" }
after(:create) do |car, evaluator|
car.style = create(:style, style_number: evaluator.car_style_number, car: car)
end
end
end
FactoryBot.define do
factory :style do
style_number { DEFAULT_STYLE_NUMBER }
end
end

Call trait from another trait with params in factory_girl

I have a fairly chunky Factory Girl trait that accepts params and creates a has_many relation. I can call that trait as part of another trait to dry up traits or make it easier to bundles traits together when passing them to factories. What I don't know how to do is how to pass params to a trait when I'm calling it from another trait, or what to do instead.
e.g.
FactoryGirl.define do
factory :currency do
name Forgery::Currency.description
sequence(:short_name) { |sn| "#{Forgery::Currency.code}#{sn}" }
symbol '$'
end
factory :price do
full_price { 6000 }
discount_price { 3000 }
currency
subscription
end
sequence(:base_name) { |sn| "subscription_#{sn}" }
factory :product do
name { generate(:base_name) }
type { "reading" }
duration { 14 }
trait :reading do
type { "reading subscription" }
end
trait :maths do
type { "maths subscription" }
end
trait :six_month do
name { "six_month_" + generate(:base_name) }
duration { 183 }
end
trait :twelve_month do
name { "twelve_month_" + generate(:base_name) }
duration { 365 }
end
factory :six_month_reading, traits: [:six_month, :reading]
factory :twelve_month_reading, traits: [:twelve_month, :reading]
trait :with_price do
transient do
full_price 6000
discount_price 3000
short_name 'AUD'
end
after(:create) do |product, evaluator|
currency = Currency.find_by(short_name: evaluator.short_name) ||
create(:currency, short_name: evaluator.short_name)
create_list(
:price,
1,
product: product,
currency: currency,
full_price: evaluator.full_price,
discount_price: evaluator.discount_price
)
end
end
trait :with_aud_price do
with_price
end
trait :with_usd_price do
with_price short_name: 'USD'
end
end
end
create(:product, :with_aud_price) # works
create(:product, :with_usd_price) # fails "NoMethodError: undefined method `with_price=' for #<Subscription:0x007f9b4f3abf50>"
# What I really want to do
factory :postage, parent: :product do
with_aud_price full_price: 795
with_usd_price full_price 700
end
The :with_price trait needs to be on a separate line from the other attributes you're setting, i.e. use this:
trait :with_usd_price do
with_price
short_name: 'USD'
end
instead of this:
trait :with_usd_price do
with_price short_name: 'USD'
end
I'm on factory_bot 4.8.2 and the following is what works for me:
trait :with_usd_price do
with_price
short_name 'USD'
end

How can I clobber factory girl associations?

I have this factory generated:
FactoryGirl.define do
sequence :account_login do |n|
"login-#{ n }"
end
factory :account do
sequence :email do |n|
"someone#{n}#gmail.com"
end
email_confirmation { |account| account.send :email }
url { Faker::Internet.url }
login { generate(:account_login) }
password { Faker::Internet.password }
password_confirmation { |account| account.send(:password) }
current_password { |account| account.send(:password) }
twitter_account 'openhub'
name { Faker::Name.name + rand(999_999).to_s }
about_raw { Faker::Lorem.characters(10) }
activated_at { Time.current }
activation_code nil
country_code 'us'
email_master true
email_kudos true
email_posts true
association :github_verification
end
end
What I really need to do is to create a second factory that creates a user with a nil github_verification attribute.
Something like this:
factory :account_with_no_verifications, parent: :account do
associaton github_verification = nil
end
Is there a way to do that?
Just use FactoryGirl::Strategy::Null http://www.rubydoc.info/github/thoughtbot/factory_girl/FactoryGirl/Strategy/Null
factory :account_with_no_verifications, parent: :account do
association github_verification, strategy: :null
end

What is the syntax to make a factory with an association that uses a trait?

I'm really trying to wrap my head around how to create complicated factories with Factory Girl, and it is NOT easy.
I have the following:
Subscription belongs_to User
Subscription belongs_to Plan
I want to test different Plans. Here is how I set it up:
FactoryGirl.define do
factory :plan do
trait :copper do
name { "Copper" }
amount { 5 }
stripe_id { "Economy" }
listing_limit { 10 }
repositories_allowed { 1 }
end
trait :copper_multi do
name { "Copper Multi" }
amount { 10 }
stripe_id { "Copper_Multi" }
listing_limit { 10 }
repositories_allowed { 5 }
end
trait :bronze do
name { "Bronze" }
amount { 5 }
stripe_id { "Basic" }
listing_limit { 10 }
repositories_allowed { 1 }
end
trait :bronze_multi do
name { "Bronze Multi" }
amount { 10 }
stripe_id { "Basic_Multi" }
listing_limit { 10 }
repositories_allowed { 5 }
end
end
end
Subscription factory is:
FactoryGirl.define do
factory :subscription do
association :user
association :plan
start_date { Time.now }
end_date { 365.days.from_now }
end
end
Naturally, this fails, because the Plan factory can't be used without also specifying a trait. This is by design.
Also, what is the difference between:
factory :subscription do
association :user
end
and:
factory :subscription do
user
end
Specify a default plan
FactoryGirl.define do
factory :plan do
name { "Default" }
amount { 1 }
stripe_id { "Default" }
listing_limit { 10 }
repositories_allowed { 1 }
trait :copper do
name { "Copper" }
amount { 5 }
stripe_id { "Economy" }
listing_limit { 10 }
repositories_allowed { 1 }
end
end
end
Now this should work
FactoryGirl.define do
factory :subscription do
association :user
association :plan
start_date { Time.now }
end_date { 365.days.from_now }
end
end
If you want to create a subscription with specific trait(copper) for plan
FactoryGirl.define do
factory :subscription do
association :user
association :plan, :factory => [:plan, :copper]
start_date { Time.now }
end_date { 365.days.from_now }
end
end
There is no difference between
factory :subscription do
association :user
end
and
factory :subscription do
user
end
But you can use the later only if the association name and the factory name matches

FactoryGirl issues with associations in Rspec/Rails

Here is the method I am testing:
class User < ActiveRecord::Base
has_many :sports, :through => :user_sports, order: "user_sports.created_at", class_name: "Sport"
has_many :user_sports
def primary_sport
return nil if user_sports.blank?
user_sports.primary_only.first.sport
end
end
User Factory;
FactoryGirl.define do
sequence(:email) do |n|
"user#{n}#example.com"
end
factory :user do
email
first_name Faker::Name.first_name
last_name Faker::Name.last_name
password "password"
password_confirmation "password"
agreed_to_age_requirements true
username "testing123"
state "AL"
city_id 201
school_id 20935
handedness "Left"
customer_id { "#{rand(1000)}" }
sports {[create(:sport)]}
after(:create) do |user, elevator|
user.subscriptions << create(:subscription)
user.roles << create(:role)
end
end
factory :athlete, class: "Athlete", parent: :user do
type "Athlete"
recruit_year "2016"
end
end
Here is my test:
require 'spec_helper'
describe User do
describe "associations" do
it { should have_and_belong_to_many(:roles) }
it { should belong_to(:account_type) }
it { should belong_to(:primary_sport).class_name("Sport") }
it { should belong_to(:school) }
it { should belong_to(:city) }
it { should belong_to(:hometown) }
it { should have_many(:social_actions) }
it { should have_one(:invitation) }
it { should have_many(:authorizations) }
it { should belong_to(:user_type) }
it { should have_and_belong_to_many(:positions).class_name "SportPosition" }
it { should have_many(:sports).through(:user_sports) }
it { should have_many(:user_sports) }
it { should have_many :contributorships }
it { should have_many(:managed_athletes).through(:contributorships) }
it { should have_and_belong_to_many(:subscriptions) }
end
describe "nested attributes" do
it { should accept_nested_attributes_for(:user_sports) }
it { should accept_nested_attributes_for(:subscriptions) }
end
describe "validations" do
it { should validate_presence_of(:email) }
it { should validate_uniqueness_of(:email) }
it { should allow_value("test#test.com").for(:email) }
it { should_not allow_value("test.com").for(:email) }
end
describe "instance methods" do
before :each do
#user = create(:user, sports: [])
#school_admin_role = create(:role, name: "School Admin")
#contributor_role = create(:role, name: "Contributor")
end
describe "#my_athletes_path" do
it "returns a school admin path if the user has the role of School Admin" do
#user.roles << #school_admin_role
#user.my_athletes_path.should eq school_admin_athletes_path
end
it "returns a school admin path if the user has the role of Contributor" do
#user.roles << #contributor_role
#user.my_athletes_path.should eq contributor_dashboard_path
end
it "returns nil if the user has no Contributor or School Admin role" do
#user.my_athletes_path.should be_nil
end
end
describe "#first_time_login?" do
it "will evalute true if the user has logged in only once" do
#user.sign_in_count = 1
#user.save
#user.first_time_login?.should be_true
end
end
describe "#confirmation_required?" do
it "returns false" do
#user.confirmation_required?.should be_false
end
end
describe "#primary_sport", focus: true do
context "when user has no primary sport" do
it "returns nil" do
#user.primary_sport.should be_nil
end
end
context "when user has a primary sport" do
it "returns sport object" do
#user.sports << create(:sport)
#user.primary_sport.should eq #user.sports.first
end
end
end
end
end
This is the error I am receiving:
Failure/Error: #user.primary_sport.should eq #user.sports.first
NoMethodError:
undefined method sport for nil:NilClass
This is because when the user_sport association is created in the User Factory, the primary column is being set to false. Not sure how to fix this. Any help is greatly appreciated! Also, sorry for the ignorance on the TDD front, Im a newb
Couldn't you just add the following to your after(:create) block in the User factory:
us = user.user_sports.first
us.primary = true
us.save
That would ensure the association gets the primary flag.

Resources