Rspec SystemStackError 堆栈级别太深
Rspec SystemStackError stack level too deep
我正在 Rails 上使用 Ruby 开发 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
如果我删除这一行,问题就会得到解决,但有人知道为什么吗?
你的序列化器中有一个循环引用:post 试图序列化它的用户,但是用户序列化器序列化用户 posts,然后序列化用户等
在 active_model_serializers 0.9.x 中有一篇关于此问题的冗长 github issue。该问题显然已在 0.10 中修复,尽管它似乎与 rails 3.x
不兼容
一种常见的技术似乎是有 2 个版本的用户序列化程序:一个包含 posts,一个不包含。
我正在 Rails 上使用 Ruby 开发 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
如果我删除这一行,问题就会得到解决,但有人知道为什么吗?
你的序列化器中有一个循环引用:post 试图序列化它的用户,但是用户序列化器序列化用户 posts,然后序列化用户等
在 active_model_serializers 0.9.x 中有一篇关于此问题的冗长 github issue。该问题显然已在 0.10 中修复,尽管它似乎与 rails 3.x
不兼容一种常见的技术似乎是有 2 个版本的用户序列化程序:一个包含 posts,一个不包含。