2017-10-06 14 views
0

私は1つの仕様と一つの工場を持っていると私はこのエラーを取得しています: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

specユーザーモデルまたはapp – eWadeMan

+0

モデルの 'app/models'です。 –

答えて

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

関連する問題