1

I have a spec:

it "should redirect to the log_books_controller#new action when a user logs in who owns zero LogBooks" do    
  visit '/auth/facebook'

  response.should redirect_to new_log_book_path
end

and it fails, with the following error:

1) LogBooksController should redirect to the log_books_controller#new action when a user logs in who owns zero LogBooks
     Failure/Error: response.should redirect_to new_log_book_path
       Expected response to be a <:redirect>, but was <200>.
     # ./spec/controllers/log_books_controller_spec.rb:7

But according to my log/test.log file, there IS actually a redirect. Am I missing something here? I feel like the spec should be passing.

log/test.log file:

Started GET "/auth/facebook/callback" for 127.0.0.1 at Thu Nov 24 10:24:01 -0600 2011
  Processing by SessionsController#create as HTML
  Parameters: {"provider"=>"facebook"}
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."uid" = '1234' AND "users"."provider" = 'facebook' LIMIT 1
   (0.0ms)  SAVEPOINT active_record_1
   (0.1ms)  SELECT 1 FROM "users" WHERE ("users"."uid" = '1234' AND "users"."provider" = 'facebook') LIMIT 1
  SQL (1.3ms)  INSERT INTO "users" ("created_at", "name", "nickname", "provider", "uid", "updated_at") VALUES (?, ?, ?, ?, ?, ?)  [["created_at", Thu, 24 Nov 2011 16:24:01 UTC +00:00], ["name", "Foo Man"], ["nickname", "fooman"], ["provider", "facebook"], ["uid", "1234"], ["updated_at", Thu, 24 Nov 2011 16:24:01 UTC +00:00]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.1ms)  SELECT COUNT(*) FROM "log_books" WHERE "log_books"."user_id" = 17
Redirected to http://rpglogger.com/log_books/new
Completed 302 Found in 35ms

I've also tried adding a debugger line after the visit '/auth/facebook' line, and at that point in the spec response.code returns :success, so I see why the test is failing, but what I don't know is why the response is set to :success when the log clearly shows a redirect.

jefflunt
  • 33,527
  • 7
  • 88
  • 126

1 Answers1

2

My guess is that Capybara follows the redirect and fetches the page specified by the location in the 302 response, which generates the 200 status code.

You could handle this by either testing for the content of the page at the redirected location, or using a controller spec with rack-test instead of Capybara.

zetetic
  • 47,184
  • 10
  • 111
  • 119
  • Sorry for the delay. This turned out to be exactly the problem (and fix). I was coming from `test_unit` which didn't follow redirects automatically, so I was working with an assumption that was no longer true. The code in the accepted answer for this question also helped me get what I needed: http://stackoverflow.com/questions/5228371/how-to-get-current-path-using-capybara – jefflunt Dec 01 '11 at 03:15