简体   繁体   中英

Rails API - RSPEC Factory Girl Stack Level Too Deep?

Encountering the following error and have been reading extensively on the stack level too deep error. I am using Ruby 1.9.3-p392, Rails 4.1.4 and Rspec-core 3.1.7 and factory girl rails 4.5 for my dev environment. I've found that some people are 'creating an infinite loop' so I've systematically commented out code to see what other errors I return, but am totally stuck.

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

Here's my spec:

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

Here's my factories

User

FactoryGirl.define do
  factory :user do
    first "MyString"
    last "MyString"
    email "MyString"
    username "MyString"
    pw "MyString"
    devicetoken "MyString"
  end
end

Notes

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

Here's the controller code:

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

Here's the Note model - do I need a validation for the uniqueness of the device token here?:

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

Your user method

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

Will recurse infinitely - I assume you meant to pass the user_id from the params.

It also seems unlikely that find_or_create_by is the right method to use - typically if you have a user id then you'd just use find

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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