2017-10-06 53 views
0

我有一個規範,一個工廠,我收到此錯誤:Rspec的和FactoryGirl:SystemStackError:堆棧層次過深軌

SystemStackError: stack level too deep 
    from gems/activerecord-4.2.1/lib/active_record/relation/delegation.rb:9:in `relation_delegate_class'  

當我嘗試使用verbage「建設」它的工作原理;但我需要它來保存以測試數據,並且'創建'在let!(:user) { create(:user) }中不起作用,因爲它引發了該錯誤。 Rspec似乎工作正常。

我正在運行ruby 2.2.2;使用RSpec的,和工廠女孩

require 'spec_helper' 
describe API::V1::CurrentUserController do 
    let!(:user) { create(:user) } 
    setup_api_authorization 

    describe "GET 'show'" do 
    before (:each) do 
    setup_api_headers 
    get 'show', subdomain: 'api', id: 'me' 
    end 
end 


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 { |user| user.password } 
    role_id {4} 
end 
end 
+1

請出示你的'User'模型。你有沒有回調函數,比如'after_create','after_commit'等等? –

+0

來自'app/models'的規範用戶模型或應用程序 – eWadeMan

+0

模型。 –

回答

0

要設置關聯你可以調用工廠的名稱:

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 

切勿硬編碼ID添加到您的工廠 - 你只是在和自己的一個世界傷害。

如果您正在使用Rolify你可以使用一個回調到一個角色添加到而不是用戶:

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 
+0

好吧,我只是想了解爲什麼堆棧層次太深了。所以它從我的模型中讀取,爲什麼會拋出這個錯誤? – eWadeMan

+0

你能解釋一下我上面寫的嗎? – eWadeMan

0

This error generally happens when you accidentally recursively changing an attribute. If you have a username attribute in User model, and a virtual attribute named username, that is directly changing the username, you end up calling the virtual, the virtual calls the virtual again and so on.. Therefore, take a look on whether something like that happens somewhere in your code.

Stack level too deep error in Ruby on Rails