Search code examples
ruby-on-railsrspecdeviserspec-rails

ActionController::UrlGenerationError for devise registration controller


I'm running into an interesting ActionController::UrlGenerationError while testing my custom devise User::RegistrationsController

Here's the error:

 ActionController::UrlGenerationError:
   No route matches {:action=>"create", :controller=>"user/registrations", :user=>{:name=>"Adriane Koepp", :city=>"Nidiafurt", :address=>"14955 Cormier Viaduct", :country=>"Mozambique", :email=>"[email protected]", :phone_primary=>"(295) 491-0447 x9108", :phone_secondary=>"536.985.9499 x7264", :postal_code=>"93438-7448", :province=>"South Carolina", :password=>"MaH9R5G8XqB", :pets_attributes=>[{:name=>"Patches", :chip_number=>"149793073311890", :species=>"iusto"}]}}

The error shows up in all my tests for this controller, but for simplicity, I'll only list one test for the create action:

it 'creates a new User' do
  expect do
    post :create, params: { user: valid_attributes }
  end.to change(User, :count).by(1)
end

The routes.rb contains:

devise_for :users, controllers: {
  registration: 'user/registration'
}

I can navigate to my registration page just fine at http://localhost:3000/users/sign_up, but for some reason, my tests don't seem to think this controller has a valid URL for the create action. Why so?

Additional steps

Upon following D1ceWard's suggestion, I pluralized "registration" in my routes, and now the error message changed to a AbstractController::ActionNotFound error. I countered that by following the documentation and added the following block to the top of my tests:

before(:each) do
  @request.env['devise.mapping'] = Devise.mappings[:user]
end

Solution

  • Your error is caused by missing pluralization, devise don't know what registration is, but work with registrations.

    Solution :

    # routes.rb
    devise_for :users, controllers: {
      registrations: 'user/registrations'
    }
    

    You can use rails routes to check all existings routes.

    Devise doc about custom controllers