ruby-on-rails – 如何编写spree控制器装饰器的测试?
发布时间:2020-12-16 23:08:35 所属栏目:百科 来源:网络整理
导读:我想修改控制器中的一些东西并使用rspec测试它们.我想为Spree :: ProductsController创建新的操作.这就是我尝试过的 routes.rbresources :productsprodcuts_controller_decorator.rbSpree::ProductsController.class_eval do before_filter :authenticate_sp
我想修改控制器中的一些东西并使用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中),您必须手动覆盖这样的路由. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |