簡體   English   中英

Rspec和FactoryGirl:SystemStackError:堆棧級別太深

[英]Rspec and FactoryGirl: SystemStackError: stack level too deep rails

我有一個規格和一個工廠,但出現此錯誤:

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

當我嘗試使用諺語“ build”時,它可以工作; 但我需要將其保存以測試數據,而在let!(:user) { create(:user) }中無法使用'create',因為它會引發該錯誤。 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

要設置關聯,您只需調用工廠名稱即可:

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

當您不小心遞歸更改屬性時,通常會發生此錯誤。 如果您在用戶模型中具有用戶名屬性,並且有一個名為用戶名的虛擬屬性(直接更改用戶名),您最終將調用該虛擬機,該虛擬機再次調用該虛擬機,依此類推。因此,請看一下是否有這樣的事情發生在代碼的某個地方。

Ruby on Rails中的堆棧級別太深錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM