What to test in Rails routing?

隐身守侯 提交于 2019-12-05 02:23:32

The biggest reasons to test routes isn't to double-test Rails, but rather to verify a public-facing API.

This reduces regressions against advertised entry points irrespective of the data they take or return.

What level to test these at is open to some debate as well; is it valuable to test the route itself, or does it only make sense to also verify data going in/coming out? Doing so also tests the same routes, and I'd argue is more valuable–but slower.

I tend to only test routes directly when:

  • They're a little funky, e.g., not bog-standard routes
  • I must reject some routes
  • During preliminary development.

Preliminary route tests generally get deleted once they're tested in other ways.

I test routes to ensure that routes I want permitted are open but also so that routes I want shut down do not work. I split the routing spec into three contexts - permitted routes, not permitted routes and custom routes.

e.g. using rspec

describe SessionsController do
  describe "routing" do
    context "permitted" do
      it "routes to #create" do

        expect(post "sessions").to route_to("sessions#create")

      end
      ...
    end
    context "custom routes" do
      it "routes 'login' to #new" do

        expect(get "/login").to route_to("sessions#new")

      end
      ...
    end
    context "invalid routes" do
      it "does not route to #edit" do

        expect(get "/sessions/edit").not_to be_routable

      end
      ...
    end
  end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!