Skip to content

Extract "@request.env['devise.mapping'] = Devise.mappings[:user]" to a test helper

We have this line:

@request.env['devise.mapping'] = Devise.mappings[:user]

in a few places in our tests, but the purpose of it is not so obvious. However, when you remove it you get:

 AbstractController::ActionNotFound:
   Could not find devise mapping for path "/users/password".
   This may happen for two reasons:

   1) You forgot to wrap your route inside the scope block. For example:

     devise_scope :user do
       get "/some/route" => "some_devise_controller"
     end

   2) You are testing a Devise controller bypassing the router.
      If so, you can explicitly tell Devise which mapping to use:

      @request.env["devise.mapping"] = Devise.mappings[:user]

We should create a test helper with a meaningful name (and a documentation possibly) to encapsulate this logic. This idea came to me when reviewing a community merge request - https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/12491#note_33797722