【问题标题】:Rails API - RSPEC Factory Girl Stack Level Too Deep?Rails API - RSPEC Factory Girl 堆栈级别太深?
【发布时间】:2014-11-10 01:33:34
【问题描述】:

遇到以下错误,并已广泛阅读堆栈级别的太深错误。我在开发环境中使用 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

【问题讨论】:

    标签: ruby-on-rails rspec factory-bot


    【解决方案1】:

    你的用户方法

    def user
      User.find_or_create_by(user_id: user.id)
    end
    

    将无限递归 - 我假设您打算从参数传递 user_id。

    find_or_create_by 似乎也不太可能是正确的使用方法 - 通常如果您有用户 ID,那么您只需使用 find

    【讨论】:

    • 也许我误解了(user_id: user.id) 的语法到底是做什么的?我假设参数应该是params[user_id],但在我更改它之前,这是在抛出PG::DatatypeMismatch: ERROR: argument of WHERE must be type boolean, not type integer
    • 你写的递归调用用户方法 - 它与 {:user_id => self.user().id} 相同。 params[:user_id] 应该可以工作 - 但请检查 params 是否包含您期望的内容。
    • 此外,除非您的用户模型有 user_id 列,否则 Activerecord 调用将失败。
    猜你喜欢
    • 2012-03-04
    • 2013-10-05
    • 2016-09-10
    • 2015-04-01
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    相关资源
    最近更新 更多