How to write tests for spree controller decorator?

I would like to change some things in the controller and test them with rspec. I want to create a new action for Spree::ProductsController . This is what I tried

 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 

But its use of the original Spree::Controller gives

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

If someone can put me in the right direction, then it will be great.

+6
source share
2 answers

Try changing the description from

 describe Spree::ProductsControllerDecorator do 

to

 describe Spree::ProductsController do 

RSpec writes a lot of material from this class. You will also want to add the following to the rspec file:

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

This will manually set up routes in RSpec to enable Spree routes. Since the route in spree / products_controller # new is not defined in your application (but in Spree instead), you will have to manually redefine your routes like this.

+6
source

in spec_helper.rb, you need to add

 require 'spree/core/testing_support/controller_requests' 

then add

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

in

 RSpec.configure do |config| 

block

explanation and courtesy http://rohanmitchell.com/2012/06/writing-controller-tests-for-spree-controllers/

0
source

Source: https://habr.com/ru/post/951301/


All Articles