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

时间:2015-10-20 03:08:02

标签: ruby-on-rails ruby rspec

我正在开发一个rails应用程序,目前正在进行测试。现在我有用户,会话和产品。用户和会话工作正常。但是当我运行我的产品测试套件(一天前运行正常)时,我现在在测试中遇到stack-level too deep错误。

我正在使用
'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 

1 个答案:

答案 0 :(得分:3)

两个序列化程序都有has_one / has_many关系。因此,每个都依赖于另一个表来确定哪个对象与之关联,从而导致堆栈溢出。

而不是在产品序列化程序中使用has_one :user,而是将其更改为belongs_to :user