Routes Testing Unit testing for Hanami routes

Routes Testing

Hanami has builtin facilities for routing unit tests.

We can assert the generated routes, to do so, we’re gonna create a spec file for the purpose. Web.routes is the class that holds all the routes for the application named Web.

It exposes a method to generate a path, which takes the name of a route as a symbol. Here’s how to test it.

# spec/web/routes_spec.rb

RSpec.describe Web.routes do
  context "path generation" do
    it "generates '/books/23'" do
      actual = subject.path(:book, id: 23)
      expect(actual).to eq("/books/23")
    end
  end

  context "route recognition" do
    it "recognizes 'PATCH /books/23'" do
      env   = Rack::MockRequest.env_for("/books/23", method: "PATCH")
      route = subject.recognize(env)

      expect(route.routable?).to be(true)

      expect(route.path).to   eq("/books/23")
      expect(route.verb).to   eq("PATCH")
      expect(route.params).to eq(id: "23")
    end
  end
end

Learn more at Routing testing guide.

Twitter Facebook

Want to learn more about Hanami?

We have written an extensive Getting Started guide for curious people like you.