Writing controller tests for Spree controllers

Without configuration, using Spree 1.1.1, rspec controller tests for spree controllers fail:

Failure/Error: get :select, :id => d.id
ActionController::RoutingError:
No route matches {:id=>"39", :controller=>"spree/distributors", :action=>"select"}

As mentioned in a related github issue, this is because the engine’s routes are not included by default in the rspec environment. However, the solution suggested there (using spree_api’s ControllerHacks module) is out of date. Instead, investigate spree_core/lib/spree/core/testing_support/controller_requests.rb, which begins with some instructions to include the helper in your rspec environment and then use the spree_get, spree_post, spree_put and spree_delete methods to access spree controller actions. In spec_helper.rb:

require 'spree/core/testing_support/controller_requests'

RSpec.configure do |config|
  config.include Spree::Core::TestingSupport::ControllerRequests, :type => :controller
end

Once I had that working, there was one other problem I ran into:

Failure/Error: spree_get :select, :id => d.id
NoMethodError:
undefined method `authenticate' for nil:NilClass

This was easily resolved by including Devise’s controller spec helpers, like so:

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

After which, my tests began running (and in this case failing) in the correct manner. Hooray!

Share and Enjoy:
  • Facebook
  • StumbleUpon
  • Twitter

Random readings...


     
     
     

    Leave a Reply

    CommentLuv Enabled