How to write tests for spree controller decorator?
Asked Answered
G

2

6

I'd like to modify some things in the controller and test them using rspec. I want to create new action for Spree::ProductsController. This is what I've 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 using original Spree::Controller and gives

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

If anyone can shove me in the right direction then it'd great.

Greenfinch answered 8/8, 2013 at 11:22 Comment(0)
B
6

Try changing your describe from

describe Spree::ProductsControllerDecorator do

to

describe Spree::ProductsController do

RSpec infers a lot of stuff from the class being described. You'll also want to add the following to the rspec file:

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

This will manually set the routes in RSpec to include the Spree routes. Since the route to spree/products_controller#new is not defined in your application (but in Spree instead) you'll have to manually override your routes like this.

Boar answered 8/8, 2013 at 22:35 Comment(4)
Sorry it was already Spree::ProductsController, corrected, but the error persistsGreenfinch
I edited the answer with an additional step that you can use. We use this in several Spree applications to test decorators with rspec.Boar
Added the routes as suggested.. still it doesn't identify the routesGreenfinch
before(:each) { @routes = Spree::Core::Engine.routes } adding this line helped. Thanks!Nonmaterial
L
1

in spec_helper.rb, you'll 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 the

RSpec.configure do |config|

block

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

Lili answered 18/6, 2015 at 22:23 Comment(1)
you can also put this in the rails_helper.rb fileLili

© 2022 - 2024 — McMap. All rights reserved.