Brad Rice
Brad Rice

Reputation: 1344

Rspec routing specs fail when route DOES exist

I'm trying to test the routing of a namespaced Rails controller action, and something isn't set up correctly.

I've already looked at the following issues, none of them even seemed to answer the questions at hand, much less give me an answer to my issue:

It's not so much that I'm passing incorrect parameters to the action, it's that the route doesn't match at all when in the context of this test. What am I missing here?

config/routes.rb (using versionist gem):

...
api_version(module: 'api/v1', path: {value: 'v1'}) do
  namespace :nphase do
    resource :device_service, controller: 'device_service', only: :none, format: :json do
      collection do
        post :get_device_information
      end
    end
  end
end
...

app/controllers/api/v1/device_service_controller.rb:

module Api                                               
  module V1                                              

    class DeviceServiceController < ApplicationController
      respond_to :json                                   

      def get_device_information                       
        respond_with json: {"success" => true}           
      end                                                
    end                                                  

  end                                                    
end                                                      

output of rake routes |grep nphase:

get_device_information_v1_nphase_device_service POST    /v1/nphase/device_service/get_device_information(.:format) api/v1/nphase/device_service#get_device_information {:format=>:json}

spec/routing/api/v1/device_service_routing_spec.rb:

require 'spec_helper'

describe 'routes for DeviceService' do
  it 'routes correctly' do
    expect(post('/v1/nphase/device_service/get_device_information')).to route_to(
      controller: 'api/v1/device_service',
      action: 'get_device_information'
    )
  end
end

Test failure:

Failure/Error: expect(post('/v1/nphase/device_service/get_device_information')).to route_to(
  No route matches "/v1/nphase/device_service/get_device_information"

Upvotes: 1

Views: 2172

Answers (1)

Jesper
Jesper

Reputation: 4555

By running your spec (albeit in Rails 4), I got this error:

Failure/Error: expect(post: '/v1/nphase/device_service/get_device_information').to route_to(
       A route matches "/v1/nphase/device_service/get_device_information", but references missing controller: Api::V1::Nphase::DeviceServiceController
     # ./spec/controller_spec.rb:5:in `block (2 levels) in <top (required)>'

This tells us that your controller isn't in the Nphase module.

By using namespace, you're telling Rails to search for the controller in particular module.

So either put the controller in the correct module (and change the directory structure accordingly), or tell rails to look for the controller in the correct place (from the Rails guides):

If you want to route /admin/articles to ArticlesController (without the Admin:: module prefix), you could use:

 scope '/admin' do
   resources :articles, :comments
 end

Upvotes: 2

Related Questions