FactoryGirl association model trouble: SystemStackError: stack level too deep
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
Rspec and FactoryGirl: SystemStackError: stack level too deep
You're getting a "stack level too deep" error because you're defining both factories in terms of each other. You don't need the association :task, factory: task
line in the question
factory -- the association will be set when you create a task.
Try this for your task
factory:
FactoryGirl.define do
factory :task do
...
questions { [ FactoryGirl.create(:question) ] }
end
end
Rails SystemStackError: stack level too deep
Let's take a look at your factories:
FactoryGirl.define do
factory :event do
name {Faker::Friends.character}
total_price 50
participant
end
factory :participant do
first_name { Faker::Name.first_name }
salary 900
event
end
end
In this scenario, the creation of event will create a participant, that will create an event, that will create a participant. and so on, in an infinite loop (stack level too deep).
Perhaps you could change it to something like this:
FactoryGirl.define do
factory :event do
name {Faker::Friends.character}
total_price 50
participants { create_list(:participant, 3, event: self) }
end
factory :participant do
first_name { Faker::Name.first_name }
salary 900
end
end
Rspec and FactoryGirl: SystemStackError: stack level too deep rails
To setup associations you can just call the name of the factory:
FactoryGirl.define do
factory :user, aliases: [:participant] do
sequence(:email) { |n| "user#{n}@example.com" }
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
password 'testpass'
password_confirmation { password }
role
end
end
Never hardcode IDs into your factories - you're only setting yourself up for a world of hurt.
If you are using Rolify you can use a callback to add a role to a user instead:
FactoryGirl.define do
factory :user, aliases: [:participant] do
sequence(:email) { |n| "user#{n}@example.com" }
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
password 'testpass'
password_confirmation { password }
after(:create) { |user| user.add_role(:peasant) }
end
end
RSPEC and factory girl SystemStackError: stack level too deep
The problem line is this:
@user = Factory(:user)
You have a circular reference: your :user
factory creates a :role
and :admin
factory. Then the :role
and :admin
factories each create another :user
factory, which then creates yet another :role
and :admin
factory, and so on until you get a stack level too deep error.
You'll need to remove the associations from some of these. I'd recommend removing the role.association
lines from both :role
and :admin
. Whenever you create a :user
, it will still create the :role
and :admin
lines for you.
Rspec SystemStackError stack level too deep
You have a circular reference in your serialiser: the post tries to serialise its user, but the user serializer serializes the users posts, which then serialise the user etc.
There is a lengthy github issue about this issue in active_model_serializers 0.9.x. The issue is apparently fixed in 0.10, although that doesn't appear to be compatible with rails 3.x
A common technique appears to be to have 2 versions of the user serializer: one which includes posts and one which does not.
SystemStackError (stack level too deep) in Model
before_save
is a callback. It runs every time you save an instance of Mine.
In assign_three_speed
, you call self.update
, which ends up up calling save
. That save call triggers another callback cycle, and the loop continues forever (until it quits with the SystemStackError).
Try assigning three_speed in the callback instead (instead of updating). The change will persist to the database because the assignment occurs before the model is saved.
def assign_three_speed
if CreateFulfillmentService::NON_US_MARKETPLACES.include?(self.marketplace)
self.three_speed = false
else
self.three_speed = true
end
end
Related Topics
Ruby: Extracting Words from String
How to Set an Option as Selected Using Selenium Webdriver (Selenium 2.0) Client in Ruby
Ruby/Sinatra - Serving Up CSS, JavaScript, or Image Files
Ruby - Determining Method Origins
How to Access (Devise) Current_User in a Rspec Feature Test
What Would a Default Getter and Setter Look Like in Rails
Rails: Copying Attributes from an Object to Another Using the "Attributes" Method
Install Any Version of Ruby with Rvm on Mavericks
How to Remove Ruby from Ubuntu
Making Multiple Http Requests Asynchronously
Empty Attribute with Ruby Haml
Can Bundler Show Me Which Gems in Gemfile Have Newer Versions (Eg. Dry-Run of Bundle Update)
Hot Deploy on Heroku with No Downtime
How to Overwrite a Getter Method in an Activerecord Model
Bundle Can't Install Rmagick Gem on MAC Osx 10.7
How to Get Rspec-2 to Give the Full Trace Associated with a Test Failure
What's the Difference Between the Build and Create Methods in Factorygirl