Is it still advisable to test routes in Rails 4 with Minitest?
After writing functional tests for a few new controllers recently, without writing any Route Integration Tests, I'm fairly confident that the additional work of testing routes is overkill. Given, that is, if there are functional tests covering all of the routes.
My reasoning is basically that... in any case where I've supplied improper parameters to a get|patch|post|delete
call into a controller action, it's always failed. And whenever I write a test for a controller action that exists but that is lacking a corresponding route it always fails. So it appears that the higher-level functional tests also exercise the lower level route integration tests just fine -- so why bother duplicating effort!
I guess I'll still be looking for an updated "official" word on this in the future, but for now I've decided to stop worrying about directly testing my routes whenever overlapping functional tests exist.
UPDATE
I've further decided that testing API routes may still be useful. This helps to ensure that the actual URL string that is published with the API is actually correct and working. Note that it's important to test the string and not the named route as the named route can change with changes to the resources in the routes file.
How can I test Rails Routes with Minitest?
If you are using minitest-rails you can create route tests by placing the following in test/routes/homepage_test.rb
:
require "minitest_helper"
class HomepageRouteTest < ActionDispatch::IntegrationTest
def test_homepage
assert_routing "/", :controller => "home", :action => "index"
end
end
Alternatively, you can use the Minitest Spec DSL:require "minitest_helper"
describe "Homepage Route Acceptance Test" do
it "resolves the homepage" do
assert_routing "/", :controller => "home", :action => "index"
end
end
You can run these tests with the following rake task:rake minitest:routes
minitest route testing
You forgot to substitute the :provider param in your route. Try this instead:
assert_routing "/auth/identity/callback",
:controller => "sessions",
:action => "create",
"provider"=>"identity"
How to test (minitest) get with locale in Rails
A little below this heading: Function Tests for your Controllers in the Rails Guide: A Guide to Testing Rails shows the get method's convention. To pass params to the method, include it as the second parameter to the call.
get :about, locale: 'en'
Need to run some code (insert into DB) before minitest route testing
After your test setup with the Fabricate
call in this case, you can request that Rails reloads its routes with:
Rails.application.reload_routes!
This should ensure that the dynamic routes are generated ahead of your assert_recognizes
call. Why are there 2 kinds of controller tests in MiniTest?
First one is Minitest::Unit test syntax explained here
Second is more like Rspec syntax, you can use minitest-spec-rails gem for that
How to test routes that don't include controller?
Try to test thus:
With Rails 3:
require 'minitest_helper'
class FooControllerTest < ActionController::TestCase
def test_should_get_index
assert_routing "/baz", :controller => "foo", :action => "bar"
end
end
With Rails 4:require "test_helper"
class FooControllerTest < ActionDispatch::IntegrationTest
def test_should_get_index
assert_generates "/baz", :controller => "foo", :action => "bar"
end
end
Related Topics
How to Reference a Local Gem from a Ruby Script
How to Use Rspec to Test That a Model Using Paperclip Is Validating The Size of an Uploaded File
Time Availability Comparison, Using Ruby on Rails
Using Elasticsearch to Filter Through Tags with Whitespace
How to Make Devise Registrationscontroller to Show Sign_Up Page Only If User Is Already Signed In
Cucumber Embed for Screenshots Not Linking to Screenshot
Git: Forcing Tests Before Pushing to Local or Remote Master
Ruby: Parse Excel 95-2003 Files
How to Evaluate a Block Inside a Proc
Sqlite3::Sqlexception: Duplicate Column Name While Migrating
How to Handle Single Table Inheritance in Simpleform So a Single Helper Handles All Models
I Need to Go from HTML to Markdown, Any Suggestion
Nlp to Classify/Label The Content of a Sentence (Ruby Binding Necesarry)
Filtering a List Using Checkboxes
Warning While Installing The Rails Plugin
Why Does Including This Module Not Override a Dynamically-Generated Method