Coder Social home page Coder Social logo

ohbarye / route_mechanic Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 3.0 60 KB

RouteMechanic detects broken routes with ease :train:

Home Page: https://rubygems.org/gems/route_mechanic

License: MIT License

Ruby 99.51% Shell 0.49%
rspec minitest ruby rails testing automation

route_mechanic's Introduction

RouteMechanic

Gem Version Build Status

No need to maintain Rails' routing tests manually. RouteMechanic automatically detects broken routes and missing action methods in controllers once you've finished installation.

Installation

Add this line to your application's Gemfile:

group :test do
  gem 'route_mechanic'
end

And then execute:

$ bundle install

Usage

RouteMechanic is available for both RSpec and MiniTest.

All you have to do is to add just one test case that keeps your application's routes not broken. Then, RouteMechanic will get to report 2 types of broken routes.

  1. Unused actions
    • Your application has the controller and the action method but config/routes.rb doesn't have corresponding settings.
  2. Unused routes
    • Your application's config/routes.rb has a routing declaration but no controller has a corresponding action method.

RSpec

Just add one test file that has only one test case using have_valid_routes matcher.

RSpec.describe 'Rails.application', type: :routing do
  it "fails if application does not have valid routes" do
    expect(Rails.application).to have_valid_routes
  end
end

If you'd like to test unused actions and unused routes separately or test only one of them, there're matchers to do so.

RSpec.describe 'Rails.application', type: :routing do
  it "fails if application has unused actions" do
    expect(Rails.application).to have_no_unused_actions
  end

  it "fails if application has unused routes" do
    expect(Rails.application).to have_no_unused_routes
  end
end

MiniTest

Just add one test file like below.

class RoutingTest < Minitest::Test
  include ::RouteMechanic::Testing::Methods

  def test_that_application_has_correct_routes
    assert_all_routes
  end
end

If you'd like to test unused actions and unused routes separately or test only one of them, there're assertions to do so.

class RoutingTest < Minitest::Test
  include ::RouteMechanic::Testing::Methods

  def test_that_application_has_no_unused_actions
    assert_no_unused_actions
  end

  def test_that_application_has_no_unused_routes
    assert_no_unused_routes
  end
end

What if RouteMechanic detects broken routes?

It tells you broken routes as follows.

  0) Rail.application fails if application does not have valid routes
     Failure/Error: expect(Rails.application.routes).to have_valid_routes

       [Route Mechanic]
         No route matches to the controllers and action methods below
           UsersController#unknown
         No controller and action matches to the routes below
           GET    /users/:user_id/friends(.:format) users#friends
           GET    /users(.:format)                  users#index
           DELETE /users/:id(.:format)              users#destroy
     # ./spec/rspec/matchers_spec.rb:8:in `block (2 levels) in <top (required)>'

1 examples, 1 failure, 0 passed

Motivation

I believe most Rails developers write request specs instead of routing specs, and you might wonder what's worth to automate routing specs. Having said that, I can come up with some use-cases of this gem.

  1. When your project is kinda aged and none knows which route is alive and which one is dead.
    • => You can detect dead code by using this gem.
  2. When your application doesn't have enough request specs (even controller specs).
    • => This gem could be a good start point to increase tests to ensure routing is valid.
  3. When you try to make a big refactor of config/routes.rb.
    • => It's a burden to run all request specs during refactoring. This could save your time.
  4. When you're compelled to write routing specs by any pressure. ;-)
    • => Set you free from tedious work!

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the RouteMechanic project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the code of conduct.

route_mechanic's People

Contributors

ohbarye avatar pocke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

route_mechanic's Issues

If parameter expects not `1`, it fails

Problem

If there's a path that expects a parameter to be a specific format, it fails.

e.g.

scope ':locale', locale: /en|ja/ do
  get '/locale_test' => 'users#index'
end
  • It expects /en|ja/,
  • But route_mechanic always gives 1 as a parameter in the path.

memo.merge({ required_part => '1' }) # '1' is pseudo id

Solution

I came up with 2 ideas as follow.

  1. Use https://github.com/tom-lord/regexp-examples and generate an example parameter that meets requirements.
  2. Remove assert_routes since it's not the main work of this gem.

I kinda want to choose the 2nd option because assert_* cause some problems like this and #7.


Thank you @pocke for reporting.

Should this gem stop using assertions provided by Rails?

Problem

First, this gem internally runs 2 checks.

  1. The gem's original aggregation of missing routes.
  2. Assertions provided by Rails

The main check process is done by 1, and it runs the 2 just in case. But the 2 causes some problems like #7 #9, and even if the 2 is skipped it can correctly report missing routes.

Therefore, I'm wondering if this gem should stop using assertions provided by Rails so that its behavior gets stable.

It doesn't work with constraints

Hi. Thanks for the great gem!

I'm trying to use this gem in my Rails application, but it raises an error with constraints.

We can reproduce the problem with the simple patch to this repository.

diff --git a/fixtures/fake_app/rails_app.rb b/fixtures/fake_app/rails_app.rb
index 09e13d7..c2b1e3d 100644
--- a/fixtures/fake_app/rails_app.rb
+++ b/fixtures/fake_app/rails_app.rb
@@ -8,6 +8,10 @@ FakeApp.config.root = File.dirname(__FILE__)
 FakeApp.initialize!
 
 FakeApp.routes.draw do
+  constraints subdomain: /\A[0-9a-z-]+\z/ do
+    get '/constraints_test' => 'users#index'
+  end
+
   resources :users do
     get 'friends', to: :friends
   end
$ bundle exec rake
# Running:

...F

Failure:
RouteMechanicTestingMethodsTest#test_that_fake_app_has_missing_routes [/home/pocke/ghq/github.com/ohbarye/route_mechanic/test/testing/methods_test.rb:63]:
--- expected
+++ actual
@@ -1,12 +1 @@
-"[Route Mechanic]
-  No route matches to the controllers and action methods below
-    UsersController#unknown
-  No controller and action matches to the routes below
-    GET    /users/:user_id/friends(.:format) users#friends
-    GET    /users(.:format)                  users#index
-    GET    /users/new(.:format)              users#new
-    GET    /users/:id/edit(.:format)         users#edit
-    GET    /users/:id(.:format)              users#show
-    DELETE /users/:id(.:format)              users#destroy
-
-"
+"No route matches \"/constraints_test\""



rails test /home/pocke/ghq/github.com/ohbarye/route_mechanic/test/testing/methods_test.rb:44

it prints No route matches "/constraints_test", but I think it is not an intentional error.


It is raised during the assert_routing method call.

assert_routing({ path: url, method: wrapper.verb }, expected_options)

All public methods in controllers are treated as action methods

Issue

If there's a public method in a controller, route_mechanic treats it as an action method that should have routes.

e.g.

# If we have a method like below,
class UsersController
  def foo_helper
  end
end

# it expects `users#foo_helper` to exist in routes

Solution

TBA ๐Ÿค”
Should users be able to ignore any action methods?

Unused route failures for actions from Rails Engines

If a Rails project contains an engine, and that engine introduces both routes and matching actions into your application, then RouteMechanic will successfully see the routes, but fail the have_no_unused_routes assertion incorrectly, because it cannot see the action.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.