简体   繁体   English

Rspec失败/错误:堆栈级别太深

[英]Rspec Failure/Error: stack level too deep

I am working on a rails app, and currently working on the tests. 我正在开发Rails应用,目前正在测试中。 Right now I have users, sessions, and products. 现在,我有用户,会话和产品。 Users and Sessions works fine. 用户和会话工作正常。 But when I run my product test suite, which was running fine a day ago, I am now getting a stack-level too deep error on my tests. 但是,当我运行一天前运行良好的产品测试套件时,我现在在测试中遇到了stack-level too deep错误。

I am using 我在用
'rspec-rails', '~> 3.3.0'
rails 4.1.8
active_model_serializers

products_controller_spec.rb 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 products_serializer.rb

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

users_serializer.rb users_serializer.rb

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

This has been the only files I have changed since I got this error. 自从出现此错误以来,这是我更改过的唯一文件。 I can post other code, but I think it's only relating to these. 我可以发布其他代码,但我认为这仅与这些有关。 Any help would be appreciated. 任何帮助,将不胜感激。

edit 编辑
stack trace 堆栈跟踪

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 

Both of your serializers have a has_one/has_many relationship. 您的两个序列化器都有has_one / has_many关系。 Because of this, each depends on the other table to determine which object(s) is associated with it, causing a stack overflow. 因此,每个表都依赖于另一个表来确定与哪个表相关联的对象,从而导致堆栈溢出。

Instead of having a has_one :user in the product serializer, change it to belongs_to :user 而不是在产品序列化器中使用has_one :user ,而是将其更改为belongs_to :user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM