Rspec - redirect_to a path matching a regexp

3.6k views Asked by At

I have the following in Rspec

response.should redirect_to %r{\A/my_settings/mysub_path/}

I get the following error

NoMethodError: undefined method `model_name' for Regexp:Class

I just need to match a part of my path which contains my_settings/mysub_path. How is this achieved in rspec?

3

There are 3 answers

0
phoet On

redirect_to expects a string or an active record model.

if you want to match something, you can use response.location

1
nfriend21 On

Here's another simple way to do it:

response.location.should =~ /whatever_you_want_to_match/
0
superluminary On

To check the redirect against a regex do a string match on the response.location, like so:

expect(response.location).to match('your_expected_location.com')