Should routing specs allow testing redirections?

9 views
Skip to first unread message

Goulven CHAMPENOIS

unread,
Apr 26, 2021, 8:07:09 AMApr 26
to rspec
Hello fellow testers,

I'm adding tests to an app that needs to redirect a lot of legacy URLs, and was surprised that routing specs do not natively allow testing redirection.

Since Rails `routes.rb` allows both routing _and_ redirecting, I presumed that routing specs would also handle both. I know controller and request specs handle redirection, but it feels weird to split tests like this, as it would lead to split my tests into request and routing specs though the path are very similar, and it's a functional unit.

I was able to test redirection by including `RSpec::Rails::RequestExampleGroup` in my routing specs but that feels weird too, and the syntax is a little different: `get '/path'` versus `get: '/path'`.

I'd feel more comfortable if I could natively write in the same file:
```
expect(get: '/blog/2018/01/slug').to route_to('posts#show')
expect(get: '/cms/2018/01/slug').to redirect_to('/blog/2018/01/slug')
```

Is there a cleaner workaround, or is it something which could be added to rspec-rails?

- Ruby version: 2.5.8
- Rails version: 6.1.3.1
- RSpec version: 3.10.1
- RSpec-Rails version:5.0.1

Jon Rowe

unread,
Apr 26, 2021, 9:47:00 AMApr 26
to RSpec
Hello!

I think the problem is that Rails itself, as far as I can see, does not provide anyway of asserting against router redirects, both the request / controller specs are using a different part of the Rails testing utils that "run" the stack (or part of in the case of controller specs) which allows them, and us, to assert on the response, (checking that its a redirect).

Routing specs by contrast use the rails routing helpers https://api.rubyonrails.org/v6.1.3.1/classes/ActionDispatch/Assertions/RoutingAssertions.html under the hood which assert on how the router recognises routes. You'd need to find a way to get them to assert a redirect to enable us to!

Cheers
Jon

On April 26, 2021, Goulven CHAMPENOIS <goul...@gmail.com> wrote:
Hello fellow testers,
I'm adding tests to an app that needs to redirect a lot of legacy URLs, and was surprised that routing specs do not natively allow testing redirection.
Since Rails `routes.rb` allows both routing _and_ redirecting, I presumed that routing specs would also handle both. I know controller and request specs handle redirection, but it feels weird to split tests like this, as it would lead to split my tests into request and routing specs though the path are very similar, and it's a functional unit.
I was able to test redirection by including `RSpec::Rails::RequestExampleGroup` in my routing specs but that feels weird too, and the syntax is a little different: `get '/path'` versus `get: '/path'`.
I'd feel more comfortable if I could natively write in the same file:```
expect(get: '/blog/2018/01/slug').to route_to('posts#show')expect(get: '/cms/2018/01/slug').to redirect_to('/blog/2018/01/slug')```

Is there a cleaner workaround, or is it something which could be added to rspec-rails?
- Ruby version: 2.5.8
- Rails version: 6.1.3.1
- RSpec version: 3.10.1
- RSpec-Rails version:5.0.1

 --
 You received this message because you are subscribed to the Google Groups "rspec" group.
 To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/e270ea61-5cdf-44ef-881c-37db783e75dan%40googlegroups.com.

Goulven CHAMPENOIS

unread,
Apr 26, 2021, 10:32:19 AMApr 26
to rs...@googlegroups.com
Ah, I see. Indeed, there doesn't seem to be a Rails assertion to test redirections like we need here.
I'll stick with my kludgy syntax then, unless someone can find a cleaner way.
Thanks for the quick response!
Reply all
Reply to author
Forward
0 new messages