【问题标题】:How to write tests for spree controller decorator?如何为 spree 控制器装饰器编写测试?
【发布时间】:2013-08-08 11:22:50
【问题描述】:

我想修改控制器中的一些内容并使用 rspec 对其进行测试。我想为Spree::ProductsController 创建new 操作。这是我尝试过的

routes.rb

resources :products

prodcuts_controller_decorator.rb

Spree::ProductsController.class_eval do
  before_filter :authenticate_spree_user!, :except => [:show, :index]


  def new
    @product = current_user.products.build
  end

end

products_controller_spec.rb

require 'spec_helper'
describe Spree::ProductsController do
  let(:user) {create(:user)}

    before(:each) do
      Spree::Core::Engine.routes
      BigPlanet::Application.routes
      controller.stub :spree_current_user => user
    end

    it "render new template" do
      get :new
      response.should render_template(:new)
    end

  end
end

但它使用原始的Spree::Controller 并给出了

Failure/Error: get :new
ActionController::RoutingError:
No route matches {:controller=>"spree/products", :action=>"new"}

如果有人能把我推向正确的方向,那就太好了。

【问题讨论】:

    标签: ruby-on-rails rspec spree


    【解决方案1】:

    尝试更改您的描述

    describe Spree::ProductsControllerDecorator do
    

    describe Spree::ProductsController do
    

    RSpec 从所描述的类中推断出很多东西。您还需要将以下内容添加到 rspec 文件中:

    before(:each) { @routes = Spree::Core::Engine.routes }
    

    这将手动设置 RSpec 中的路线以包含 Spree 路线。由于 spree/products_controller#new 的路由没有在您的应用程序中定义(而是在 Spree 中),您必须像这样手动覆盖您的路由。

    【讨论】:

    • 抱歉已经是Spree::ProductsController,已更正,但错误仍然存​​在
    • 我用您可以使用的附加步骤编辑了答案。我们在几个 Spree 应用程序中使用它来测试带有 rspec 的装饰器。
    • 按照建议添加路线.. 仍然无法识别路线
    • before(:each) { @routes = Spree::Core::Engine.routes } 添加此行有帮助。谢谢!
    【解决方案2】:

    在 spec_helper.rb 中,您需要添加

    require 'spree/core/testing_support/controller_requests'
    

    然后,添加

    config.include Spree::Core::TestingSupport::ControllerRequests, :type => :controller
    config.include Devise::TestHelpers, :type => :controller
    

    RSpec.configure do |config|
    

    阻止

    解释和礼貌http://rohanmitchell.com/2012/06/writing-controller-tests-for-spree-controllers/

    【讨论】:

    • 你也可以把这个放到rails_helper.rb文件中
    猜你喜欢
    • 2014-09-20
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 2011-10-03
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多