Andrei
Andrei

Reputation: 1191

Rails 5 routing spec requires :path value

After upgrading Rails 4.2.8 to 5.0, all of my routing specs are failing with the same error message:

1) UsersController routing routes to SHOW
     Failure/Error: expect(get: '/users/joe-smith').to route_to('users#show', id: 'joe-smith')

       The recognized options <{"path"=>"users/joe-smith", "controller"=>"users", "action"=>"show", "id"=>"joe-smith"}> did not match <{"id"=>"joe-smith", "controller"=>"users", "action"=>"show"}>, difference:.
       --- expected
       +++ actual
       @@ -1 +1 @@
       -{"id"=>"joe-smith", "controller"=>"users", "action"=>"show"}
       +{"path"=>"users/joe-smith", "controller"=>"users", "action"=>"show", "id"=>"joe-smith"}
     # ./spec/routing/users_controller_routing_spec.rb:10:in `block (3 levels) in <top (required)>'

Finished in 0.05154 seconds (files took 6.86 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/routing/users_controller_routing_spec.rb:9 # UsersController routing routes to SHOW

The spec:

it 'routes to SHOW' do
  expect(get: '/users/joe-smith').to route_to('users#show', id: 'joe-smith')
end

Anyone have any insight into WHY this is happening? I didn't see anything in the Rails 5 CHANGELOG.

Upvotes: 1

Views: 481

Answers (1)

max
max

Reputation: 102222

A better fix is to start the move to a more future proof method of testing that provides more value:

require 'rails_helper'
RSpec.describe "Users", type: :request do
  let(:user) { create(:user) }

  describe "GET /users/:id" do
    it "takes a username as the id param" do
       get user_path(user.user_name)
       expect(response).to be_successful
    end
  end
end

Upvotes: 2

Related Questions