【发布时间】:2016-09-10 05:33:12
【问题描述】:
我正在使用 Ruby on Rails 开发一个 API。我为 posts_controller.rb 创建了一些规范,但在运行规范时出现此错误
SystemStackError: stack level too deep
./app/controllers/api/v1/posts_controller.rb:10:in `show'
./spec/controllers/api/v1/posts_controller_spec.rb:8:in `block (3 levels) in <top (required)>'
这是我的posts_controller_spec.rb
require 'spec_helper'
describe API::V1::PostsController do
describe "GET #show" do
before(:each) do
@post = FactoryGirl.create :post
get :show, id: @post.id
end
it "returns the information about a post on a hash" do
post_response = json_response[:post]
expect(post_response[:description]).to eql @post.description
end
it "has the user as a embeded object" do
post_response = json_response[:post]
expect(post_response[:user][:email]).to eql @post.user.email
end
it { expect(response.status).to eql 200 }
end
.
.
.
这是我的posts_controller.rb
class API::V1::PostsController < ApplicationController
respond_to :json
def show
respond_with Post.find(params[:id])
end
.
.
.
有人有解决这个问题的想法吗?
我意识到这是导致错误的行,有人知道为什么吗?在post_serializer.rb 文件中我有这个
class PostSerializer < ActiveModel::Serializer
attributes :id, :description, :price, :published
has_one :user # this is the line !!!
end
如果我删除此行,问题将得到解决,但有人知道为什么吗?
【问题讨论】:
-
json_response是做什么的?还有,工厂有什么不寻常的地方吗? -
json_response是一个辅助方法,它返回: JSON.parse(response.body, symbolize_names: true) -
用户序列化程序中有
has_many :posts吗?如果你有它,那和后序列化程序中的has_one :user很可能是堆栈级别太深错误的原因。 -
请展示你的factory_girl stab和model
-
我怀疑@RomanKovtunenko 是在正确的轨道上。你的
post工厂可能会调用你的user工厂,它可能会调用你的post工厂,然后它会调用你的user工厂,等等。无限
标签: ruby-on-rails ruby ruby-on-rails-3 rspec