How to Assert That No Route Matches in a Rails Integration Test

How can I assert that no route matches in a Rails integration test?

I ended up doing this:

  def test_no_routes_match_when_neither_foo_nor_bar_exist
assert_raises(ActionController::RoutingError) do
assert_recognizes({}, '/category/this-is-neither-a-foo-nor-a-bar')
end
end

Slightly silly, but it gets the job done.

Note that this does not work with Rails 4. See the answer below for a Rails 4 solution.

No route matches in functional/controller test

The solution to this problem was much simpler than expected:

# change this:
get(root_path)

# to this
get(:index)

The with_routing method works fine to define the path in this context.

Route does not match in test but is recognized in application

The tutorial is defining an Integration Test (inherits from ActionDispatch::IntegrationTest), whereas your above code is defining a Controller Test (inherits from ActionController::TestCase).

get :edit, ... is the correct syntax for a controller test, because it bypasses URL recognition and directly specifies the :action. This is confusing, and is one of several reasons that controller tests are now discouraged in favour of integration tests, which is probably what you want to create.

To do so, change:

class UsersControllerTest < ActionController::TestCase

to:

class UsersControllerTest < ActionDispatch::IntegrationTest

(Note the tutorial, somewhat confusingly, uses ActionDispatch::IntegrationTest as the base class both for tests it puts in tests/integration/ and those it puts in tests/controllers/.)

Rails4 - Testing - URLGenerationError: No route matches

The test methods get, post, delete, etc., all assume that any parameter you pass them is the name of an action within the controller being tested. So, this call works:

get :new

because it generates url_for(:controller => :sessions, :action => :new). This call doesn't:

get '/login'

because it generates url_for(:controller => :sessions, :action => '/login').

Use the symbol form.

Hartl Ruby on Rails Tutorial - Chapter 8 failed test

In your routes.rb

get 'login'   => 'sessions#new'
get 'login' => 'sessions#create'

that looks weird. I guess it should be

get 'login'   => 'sessions#new'
post 'login' => 'sessions#create'

ActionController::RoutingError: No route matches [POST]

It's really hard to tell what's going on here. In general if you are asking a question about a routing error you should post what's in your routes.rb file as well.

That being said, I think whatever HTML gets generated for the form has it's action specified incorrectly.

Example routes:

    tags GET    /tags(.:format)                {:action=>"index", :controller=>"tags"}
POST /tags(.:format) {:action=>"create", :controller=>"tags"}
new_tag GET /tags/new(.:format) {:action=>"new", :controller=>"tags"}
edit_tag GET /tags/:id/edit(.:format) {:action=>"edit", :controller=>"tags"}
tag GET /tags/:id(.:format) {:action=>"show", :controller=>"tags"}
PUT /tags/:id(.:format) {:action=>"update", :controller=>"tags"}
DELETE /tags/:id(.:format) {:action=>"destroy", :controller=>"tags"}

Notice where it says POST in the second column there. That means the action attribute for a new object form should be set to /tags. Having that there tells Rails to render the create action in the Tags controller. The same would be true for your login model.

As far as what your form HTML code actually looks like, it probably looks something along the lines of:

<form ... action="/logins/new" ...>...</form>

When it should be

<form ... action="/logins" ...>...</form>

Hope this helps.

No route found in rspec test for an existing route

It looks like you are writing a controller spec (which rails calls functional tests)

In these tests the get, post etc methods expect the first argument to be the name of the action and the second a hash of options - they bypass routing (although they do check that the action is routable). You would instead do

get :show, id: e.id

In integration tests (request specs or feature specs) on the other hand you would use an actual path (and depending on the setup you would use either visit or get, post etc but they'd be different get methods)



Related Topics



Leave a reply



Submit