ruby-on-rails – 如何编写spree控制器装饰器的测试?

我想修改控制器中的一些东西并使用rspec测试它们.我想为Spree :: ProductsController创建新的操作.这就是我尝试过的
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"}

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

解决方法

尝试更改您的描述
describe Spree::ProductsControllerDecorator do

describe Spree::ProductsController do

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

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

这将手动设置RSpec中的路由以包括Spree路由.由于未在您的应用程序中定义spree / products_controller #new的路由(但在Spree中),您必须手动覆盖这样的路由.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...