微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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中),您必须手动覆盖这样的路由.

原文地址:https://www.jb51.cc/ruby/265057.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐