0
我正在开发一个使用Ruby on Rails的API。我已经创造了一些规格为posts_controller.rb
,当运行规范我有这个错误Rspec SystemStackError堆栈级别太深
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'做什么?另外,工厂有什么不寻常的吗? – zetetic
'json_response'是一个帮助方法,返回这个:JSON.parse(response.body,symbolize_names:true) – crespitowil
你有'UserSerializer'吗?如果是这样,请显示它的内容。 –