Such Dev Blog
Who am I ?
Portfolio
Resources
Lessons
Opinions
Tutorials
Who am I ?
Portfolio
Resources
Lessons
Opinions
Tutorials
  • Who am I ?
  • Portfolio
  • Resources

    • πŸ‘Ά Resources for the Beginner WebDev
    • πŸ‘© Resources for the Intermediate WebDev
    • πŸ‘΄ Resources for the Senior WebDev
    • πŸ§‘β€πŸŽ¨ Design resources for WebDevs
    • πŸ€– LLM / AI Resources
  • Lessons

    • πŸ“ˆ Productivity and Well-being, A summary of what works.
    • ⭐ Dramatically increase your productivity with Atomic Git Commits
    • πŸ§‘β€πŸ’Ό How to Learn Git Slowly
    • 😠 How to Start Learning CSS without hating yourself
    • 🧠 How to be a great software engineer without using your brain
    • ⁉️ Explaining Ruby's Singleton Class (Eigenclass) to confused beginners
  • Opinions

    • πŸ”ͺ The Technical Debt explained with a kitchen analogy
    • πŸͺ  Why our work culture sucks
    • 🧹 The Marie Kondo guide for the Clean Developer
    • πŸ’‘What contributing to Open-source is, and what it isn't
    • πŸ’΅ Why diversity is important, no, really, actually for real
    • πŸ•Έ Networking is easy, fun, and probably not what you think it is.
  • Tutorials

    • πŸ“ Build your own system with ArchLinux
    • πŸ’½ Testing Ansible scripts with Vagrant
    • πŸ’Ž Upload Files from Vue.js to Rails with ActiveStorage
    • πŸ’Ž Filter Anything in a Rails request in 10 lines of code
    • πŸ’Ž Test all your GET routes in rails with 20 lines of code
    • πŸ’Ž How to use Solr / Sunspot with Rails
    • πŸ’Ž Debugging Solr Sunspot like a pro

πŸ’Ž Test all your GET routes in rails with 20 lines of code

What and Why?

A great friend of mine (who also happen to be a great rails developer) told me once a great trick to increase your code coverage easily. You can just write a test to check if all your GET routes return a non-errored code.

Indeed, the most important information to check about our static GET pages is to know if they are currently erroring or not. So a quick test to check if those pages are working or not is a great low-hanging fruit.

The code itself

To be run with the RSpec framework:

# frozen_string_literal: true

require 'rails_helper'

describe Rails.application.routes do
  describe '#get', type: :request do
    let(:routes_info) { described_class.routes }
    let(:routes_get) { routes_info.select { |r| r.verb == 'GET' } }
    let!(:user) { create(:user) }

    specify 'GET routes returns 200, 301 or 302 code' do
      routes_get.each do |route|
        route = route.path.spec.to_s.gsub(/\(.:format\)*$/, '')

        get route_with_params(route, user)
        expect(response.status).to eq(200).or eq(301).or eq(302)
      end
    end

    def route_with_params(route, user)
      route.gsub!(':user_id', user.id)
    end
  end
end

And that's pretty much it ! As you can see, we just extract all the routes from the Rails application and check that they return a valid code.

There is but one slightly complex point: some of the routes tested might need a parameter, such as a user ID. We use the method route_with_params to inject a user ID in those routes.

VoilΓ , you might have gotten a great boost to your code coverage with just 20 easy lines of code!

Happy coding to all!

Last Updated:
Contributors: Samuelfaure
Prev
πŸ’Ž Filter Anything in a Rails request in 10 lines of code
Next
πŸ’Ž How to use Solr / Sunspot with Rails