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'嗎?如果是這樣,請顯示它的內容。 –