1
votes

I've started writing a spec for a new controller. By rights, the action for this controller should not be routeable. I have not written a route for it. Yet, somehow rspec manages to route to the action and run it.

I am very puzzled.

Here’s a spec and console output which show that this should not be routable:

it 'accepts POSTs to receiver' do
   { post: "http://api.customersure.com/webhooks/foobar/receiver" }.should route_to(controller: 'webhooks/foobar', action: 'receiver')
end

This fails, and it should fail as I haven’t added that route.

However, this controller spec passes:

describe Webhooks::FoobarController, type: :controller do
  describe "POST receiver" do
    it "returns 200 OK" do
      post :receiver
      expect(response.status).to eq(200)
    end
  end
end

I've quickly verified that it’s definitely routing to the #receiver action in the Foobar controller by inserting a puts into the action. The string from that puts appears in my console when I run the test.

So…

  1. Most helpful to me – can anyone explain how rspec is managing to route to this action?
  2. In general, how can I debug this? How can I work backwards from an action to work out what routes were followed?

Thanks!

1

1 Answers

2
votes

Rspec in controller spec does not go through the routing stack (unlike feature or request specs).

Controller specs directly tap into your controllers actions.

You can also use hide_action :receiver on top of your controller class to hide the action. And if you need to test that your action is not routeable then use a feature test.