0

I'm adding to test to an existing project. I have a user class that is assigned a unique url on create ad then is redirected to a path that contains that url.

I'm following the rspec book and wrote this test

it 'redirects to users#show' do
  post :create, user: attributes_for(:guest)
  expect(response).to redirect_to guest_path(:guest)
end

I get this back when I run the test.

Expected response to be a redirect to <http://test.host/guest/guest> but was a redirect to <http://test.host/guest/h7gutr1CMYxa5VNgWH5l1A>

The redirect the controller is sending back is correct but I am obviously writing the test incorrectly. How can I alter the expectation to pass the test?

Edit: Got a little closer, but still not there. Using

expect(response).to redirect_to guest_path(assigns(:user))

gives:

Expected response to be a redirect to <http://test.host/guest/1> but was a redirect to <http://test.host/guest/zaOVsiPBwQ4yNnC0mJNoNA>

using:

expect(response).to redirect_to guest_path(assigns(:user))

gives:

NoMethodError:
   undefined method `url' for :user:Symbol

Solution:

I couldn't figure out how to call methods on the instance variable. It's done like this:

it 'redirects to users#show' do
  post :create, user: attributes_for(:guest)
  expect(response).to redirect_to guest_path(assigns(:user).url)
end
5
  • 1
    Did you use assigns(:guest) rather than assigns(:user)? Commented Sep 6, 2013 at 20:22
  • I've tried with both. Using :user sends me to an id, but this redirect uses the objects url instead of the id. Commented Sep 6, 2013 at 20:30
  • 1
    Can you post your controller method? Commented Sep 6, 2013 at 20:32
  • I just got it. Thanks for putting me on the right path with assigns. Commented Sep 6, 2013 at 20:34
  • I know you solved this and of course that's great. But I'm thinking here why that route is working with a string as a parameter instead of an ActiveRecord object or a hash... Commented Sep 7, 2013 at 0:23

1 Answer 1

2

Try this:

it 'redirects to users#show' do
  post :create, user: attributes_for(:guest)
  expect(response).to redirect_to guest_path(assigns(:guest))
end

In this case, assigns(:guest) will be equal to the instance variable with the same name as the arguments (:guest) that was returned by the request you're testing for.

Sign up to request clarification or add additional context in comments.

4 Comments

I initially used something similar form the rspec book. I get an error ActionController::RoutingError: No route matches {:controller=>"users", :action=>"guest", :url=>nil} But thanks.
Hey, don't be so quick to assume the test must be wrong. You may have some routing problem in there.
The test output is exactly what I want. (test.host/guest/zaOVsiPBwQ4yNnC0mJNoNA) But the way I've written the test, it thinks I want something else back.
Please note it's assigns(:guest), not assigns(:user).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.