Question

I get the following "No route matches" error with minitest/minitest-rails:

  1) Error:
SessionsController::#create#test_0002_get authenticated user session:
ActionController::UrlGenerationError: No route matches {:action=>"create", :controller=>"sessions"}
    test/controllers/sessions_controller_test.rb:30:in `block (3 levels) in <top (required)>'

But the others get :new and get :destroy routes are fine. This is test/controllers/sessions_controller_test.rb

require "test_helper"

describe SessionsController do
  describe "#new" do
    it "GET, /signin" do
      get :new
      assert_response 302
    end

    it "GET, /auth/github" do
      get :new
      assert_redirected_to '/auth/github'
    end
  end

  describe "#create" do
    specify { request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:github] }

    it "get authenticated user session" do

  --> get :create
      ...
    end
  end

   describe "#destroy" do
    it "GET, /signout and reset_session" do
      get :destroy
      session[:user_id].must_equal nil
    end

    it "GET, / by redirect_to root_url" do
      get :destroy
      assert_response 302
      assert_redirected_to root_path
    end
  end

end

and this is the controller:

class SessionsController < ApplicationController
  def new
    redirect_to '/auth/github'
  end

  def create
    auth = request.env["omniauth.auth"]
    user = User.where(:token => auth['credentials']['token']).first ||
           User.create_with_omniauth(auth)
    reset_session
    session[:user_id] = user.id.to_s
  end

  def destroy
    reset_session
    redirect_to root_url, :notice => 'Signed out!'
  end
end

the routes:

$ rake routes|grep session
auth_github_callback GET   /auth/:provider/callback(.:format) sessions#create
              signin GET   /signin(.:format)                  sessions#new
             signout GET   /signout(.:format)                 sessions#destroy

$ grep session config/routes.rb:

  get '/auth/:provider/callback' => 'sessions#create', :as => :auth_github_callback
  get '/signin' => 'sessions#new', :as => :signin
  get '/signout' => 'sessions#destroy', :as => :signout

extracting minitest relevancies from Gemfile.lock:

      minitest (~> 5.1)
    minitest (5.3.3)
    minitest-capybara (0.6.1)
      minitest (~> 5.0)
    minitest-metadata (0.5.0)
      minitest (>= 4.7, < 6.0)
    minitest-rails (2.0.1)
      minitest (>= 5.3.3, < 6.0)
    minitest-rails-capybara (2.0.0)
      minitest-capybara (~> 0.6.1)
      minitest-metadata (~> 0.5.0)
      minitest-rails (~> 2.0.0)
  minitest-rails-capybara

the app runs on Rails 4.1.0 / ruby 2.1.1p76.

The application works fine in development, that make me think something goes wrong with test routing someway, some routes are fine and just create fails.

Until now, I've no idea where to look at.

Was it helpful?

Solution

The route is expecting :provider to be present. You aren't specifying the value and so the router can't match the request to the controller action. Try changing your test to the following:

get :create, provider: "github"

This is the same error you will get if you call a show action without providing the :id value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top