繁体   English   中英

Rails API-RSPEC工厂女孩堆栈级别是否太深?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM