[英]Rails API - RSPEC Factory Girl Stack Level Too Deep?
遇到以下錯誤,並且已經在堆棧級別上廣泛閱讀過深的錯誤。 我在我的開發環境中使用的是Ruby 1.9.3-p392,Rails 4.1.4和Rspec-core 3.1.7和factory girl rails 4.5。 我發現有些人正在“創建無限循環”,因此我系統地注釋掉了代碼以查看我還返回了哪些其他錯誤,但是完全卡住了。
1) POST /v1/notes/id saves the lat, lon, note text, note photo, recipients, and expiration
Failure/Error: Unable to find matching line from backtrace
SystemStackError:
stack level too deep
# /Users/njayanty/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/activesupport- 4.1.4/lib/active_support/cache/strategy/local_cache_middleware.rb:33
#
# Showing full backtrace because every line was filtered out.
# See docs for RSpec::Configuration#backtrace_exclusion_patterns and
# RSpec::Configuration#backtrace_inclusion_patterns for more information.
Finished in 0.52889 seconds (files took 9.32 seconds to load)
8 examples, 1 failure
這是我的規格:
describe 'POST /v1/notes/id' do
it 'saves the lat, lon, note text, note photo, recipients, and expiration' do
date = Time.zone.now
devicetoken = '123abcd456xyz'
user = create(:user, devicetoken: devicetoken)
post '/v1/notes', {
user_id: user.id,
devicetoken: user.devicetoken,
lat: 5.5,
lon: 3.3,
note_text: 'Hey sweetie! Just thinking about your lovely face!',
photo_uri: 'http://www.photour/l12345.jpg',
expiration: date,
}.to_json, {'Content-Type' => 'application/json' }
note = Note.last
expect(response_json).to eq({ 'id' => note.id })
expect(note.lat).to eq (5.5)
expect(note.lon).to eq(3.3)
expect(note_text).to eq('Hey sweetie! Just thinking about your lovely face!')
expect(note.photo_uri).to eq('http://www.photour/l12345.jpg')
expect(note.expiration.to_i).to eq date.to_i
end
end
這是我的工廠
用戶
FactoryGirl.define do
factory :user do
first "MyString"
last "MyString"
email "MyString"
username "MyString"
pw "MyString"
devicetoken "MyString"
end
end
筆記
FactoryGirl.define do
factory :note do
user_id 1
note_text "MyText"
lat 1.5
lon 1.5
photo_uri "MyString"
expiration Time.zone.now.utc
end
end
這是控制器代碼:
def note_params
{
user_id: user.id,
devicetoken: user.devicetoken,
lat: params[:lat],
lon: params[:lon],
note_text: params[:note_text],
photo_uri: params[:photo_uri],
expiration: params[:expiration]
}
end
def user
User.find_or_create_by(user_id: user.id)
end
這是Note模型-這里是否需要驗證設備令牌的唯一性?
class Note < ActiveRecord::Base
validates :user_id, presence: true
validates :note_text, presence:true
validates :lat, presence: true
validates :lon, presence: true
validates :expiration, presence: true
belongs_to :user, foreign_key: 'user_id', class_name: 'User'
end
您的用戶方法
def user
User.find_or_create_by(user_id: user.id)
end
將無限遞歸-我假設您打算從參數中傳遞user_id。
似乎不太可能使用find_or_create_by
是正確的方法-通常,如果您具有用戶ID,則只需使用find
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.