2015-10-20 108 views
0

我正在開發一款Rails應用程序,目前正在進行測試。現在我有用戶,會話和產品。用戶和會話工作正常。但是當我運行一天前運行良好的產品測試套件時,現在我的測試中出現了stack-level too deep錯誤。Rspec失敗/錯誤:堆棧級別太深

我使用
'rspec-rails', '~> 3.3.0'
rails 4.1.8
active_model_serializers

products_controller_spec.rb

require 'rails_helper' 
describe Api::V1::ProductsController, :controller => true do 
    describe "#GET show" do 
     before(:each) do 
      @product = FactoryGirl.create :product 
      get :show, id: @product.id 
     end 

     it "returns the product information of a certain id" do 
      product_response = json_response[:product] 
      expect(product_response[:title]).to eql @product.title 
     end 

     it "has a user as an embedded object" do 
      product_response = json_response[:product] 
      expect(product_response[:user][:email]).to eql @product.user.email 
     end 
     it { should respond_with 200 } 
    end 

    describe "#GET index" do 
     # added collection matchers (have(4)) gem, was core now a gem 
     before(:each) do 
      4.times { FactoryGirl.create :product } 
      get :index 
     end 

     # setting up to return the scoped product records 

     it "returns 4 unique products" do 
      products_response = json_response 
      expect(products_response[:products]).to have(4).items 
     end 

     it "returns the user object into each product" do 
      products_response = json_response[:products] 
      products_response.each do |product_response| 
       expect(product_response[:user]).to be_present 
      end 
     end 
     it { should respond_with 200 } 
    end 

    describe "POST #create" do 
     context "when it is successfully created" do 
     before(:each) do 
      user = FactoryGirl.create :user 
      @product_attributes = FactoryGirl.attributes_for :product 
      api_authorization_header user.auth_token 
      post :create, { user_id: user.id, product: @product_attributes } 
     end 

     it "renders the json response for the product created" do 
      product_response = json_response[:product] 
      expect(product_response[:title]).to eql @product_attributes[:title] 
     end 

     it { should respond_with 201 } 
     end 

     context "when it is not created" do 
     before(:each) do 
      user = FactoryGirl.create :user 
      @invalid_product_attributes = { title: "Smart TV", price: "Twelve dollars" } 
      api_authorization_header user.auth_token 
      post :create, { user_id: user.id, product: @invalid_product_attributes } 
     end 

     it "renders an errors json" do 
      product_response = json_response 
      expect(product_response).to have_key(:errors) 
     end 

     it "renders the json errors on why the product could not be created" do 
      product_response = json_response 
      expect(product_response[:errors][:price]).to include "is not a number" 
     end 

     it { should respond_with 422 } 
     end 
    end 

    describe "PUT/PATCH #update" do 
     before(:each) do 
     @user = FactoryGirl.create :user 
     @product = FactoryGirl.create :product, user_id: @user.id 
     api_authorization_header @user.auth_token 
     end 

     context "when a product is successfully updated" do 
     before(:each) do 
      patch :update, { user_id: @user.id, id: @product.id, 
      product: { title: "A Kind of TV" } } 
     end 

     it "renders the json response for the product updated" do 
      product_response = json_response[:product] 
      expect(product_response[:title]).to eql "An expensive TV" 
     end 

     it { should respond_with 200 } 
     end 

     context "when a product is not successfully updated" do 
     before(:each) do 
      patch :update, { user_id: @user.id, id: @product.id, 
      product: { price: "A Kind of thing" } } 
     end 

     it "renders a json error" do 
      product_response = json_response 
      expect(product_response).to have_key(:errors) 
     end 

     it "renders a json error explaining why" do 
      product_response = json_response 
      expect(product_response[:errors][:price]).to include "is not a number" 
     end 

     it { should respond_with 422 } 
     end 
    end 



    describe "DELETE #destroy" do 
     before(:each) do 
      @user = FactoryGirl.create :user 
      @product = FactoryGirl.create :product, user: @user 
      api_authorization_header @user.auth_token 
      delete :destroy, { user_id: @user.id, id: @product.id } 
     end 
     it { should respond_with 204 } 
    end 
end 

products_serializer.rb

class ProductSerializer < ActiveModel::Serializer 
    attributes :id, :title, :published 
    has_one :user 
end 

users_serializer.rb

class UserSerializer < ActiveModel::Serializer 
    embed :ids 
    attributes :id, :email, :created_at, :updated_at, :auth_token 
    has_many :products 
end 

這一直以來我得到這個錯誤我已經改變了唯一的文件。我可以發佈其他代碼,但我認爲它只涉及這些。任何幫助,將不勝感激。

編輯
堆棧跟蹤

DEPRECATION WARNING: ** Notice: embed is deprecated. ** 
The use of .embed method on a Serializer will be soon removed, as this should have a global scope and not a class scope. 
Please use the global .setup method instead: 
ActiveModel::Serializer.setup do |config| 
    config.embed = :ids 
    config.embed_in_root = false 
end 
(called from <class:UserSerializer> at /Users//marketplaceapi/app/serializers/user_serializer.rb:2) 
FFFFFFFF...FF.... 

Failures: 

    1) Api::V1::ProductsController#GET show returns the product information of a certain id 
    Failure/Error: Unable to find matching line from backtrace 
    SystemStackError: 
     stack level too deep 
    # /Users//.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/notifications/instrumenter.rb:23 
    # 
    # 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. 

    2) Api::V1::ProductsController#GET show has a user as an embedded object 
    Failure/Error: Unable to find matching line from backtrace 
    SystemStackError: 
     stack level too deep 
    # /Users//.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/notifications/instrumenter.rb:23 
    # 
    # 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. 

    3) Api::V1::ProductsController#GET show 
    Failure/Error: Unable to find matching line from backtrace 
    SystemStackError: 
     stack level too deep 
    # /Users//.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/notifications/instrumenter.rb:23 
    # 
    # 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. 

    4) Api::V1::ProductsController#GET index returns 4 unique products 
    Failure/Error: Unable to find matching line from backtrace 
    SystemStackError: 
     stack level too deep 
    # /Users//.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/notifications/instrumenter.rb:23 
    # 
    # 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. 

    5) Api::V1::ProductsController#GET index returns the user object into each product 
    Failure/Error: Unable to find matching line from backtrace 
    SystemStackError: 
     stack level too deep 
    # /Users//.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/notifications/instrumenter.rb:23 
    # 
    # 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. 

    6) Api::V1::ProductsController#GET index 
    Failure/Error: Unable to find matching line from backtrace 
    SystemStackError: 
     stack level too deep 
    # /Users//.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/notifications/instrumenter.rb:23 
    # 
    # 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. 

    7) Api::V1::ProductsController POST #create when it is successfully created renders the json response for the product created 
    Failure/Error: Unable to find matching line from backtrace 
    SystemStackError: 
     stack level too deep 
    # /Users//.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/notifications/instrumenter.rb:23 
    # 
    # 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. 

    8) Api::V1::ProductsController POST #create when it is successfully created 
    Failure/Error: Unable to find matching line from backtrace 
    SystemStackError: 
     stack level too deep 
    # /Users//.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/notifications/instrumenter.rb:23 
    # 
    # 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. 

    9) Api::V1::ProductsController PUT/PATCH #update when a product is successfully updated renders the json response for the product updated 
    Failure/Error: Unable to find matching line from backtrace 
    SystemStackError: 
     stack level too deep 
    # /Users//.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/notifications/instrumenter.rb:23 
    # 
    # 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. 

    10) Api::V1::ProductsController PUT/PATCH #update when a product is successfully updated 
     Failure/Error: Unable to find matching line from backtrace 
     SystemStackError: 
     stack level too deep 
     # /Users//.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/notifications/instrumenter.rb:23 
     # 
     # 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 2.14 seconds (files took 3.17 seconds to load) 
17 examples, 10 failures 

Failed examples: 

rspec ./spec/controllers/api/v1/products_controller_spec.rb:13 # Api::V1::ProductsController#GET show returns the product information of a certain id 
rspec ./spec/controllers/api/v1/products_controller_spec.rb:19 # Api::V1::ProductsController#GET show has a user as an embedded object 
rspec ./spec/controllers/api/v1/products_controller_spec.rb:25 # Api::V1::ProductsController#GET show 
rspec ./spec/controllers/api/v1/products_controller_spec.rb:37 # Api::V1::ProductsController#GET index returns 4 unique products 
rspec ./spec/controllers/api/v1/products_controller_spec.rb:43 # Api::V1::ProductsController#GET index returns the user object into each product 
rspec ./spec/controllers/api/v1/products_controller_spec.rb:50 # Api::V1::ProductsController#GET index 
rspec ./spec/controllers/api/v1/products_controller_spec.rb:62 # Api::V1::ProductsController POST #create when it is successfully created renders the json response for the product created 
rspec ./spec/controllers/api/v1/products_controller_spec.rb:68 # Api::V1::ProductsController POST #create when it is successfully created 
rspec ./spec/controllers/api/v1/products_controller_spec.rb:109 # Api::V1::ProductsController PUT/PATCH #update when a product is successfully updated renders the json response for the product updated 
rspec ./spec/controllers/api/v1/products_controller_spec.rb:115 # Api::V1::ProductsController PUT/PATCH #update when a product is successfully updated 
+0

這將是一個漫長的讀取,你可以發佈您的堆棧跟蹤呢?這將有助於縮小問題的範圍。 – vee

+0

@vee haha​​,ya在過去的幾天裏,我一直在嘲笑我的頭髮。我發佈堆棧跟蹤 –

回答

3

您的串行雙方都有一個HAS_ONE /的has_many關係。因此,每個都依賴於另一個表來確定哪個對象與它關聯,導致堆棧溢出。

而是在產品序列化有has_one :user的,將其更改爲belongs_to :user

+0

很好,感謝您的幫助。這解決了它。 –

相關問題