日志显示'重定向',但RSpec看到'成功'

时间:2011-11-24 16:33:51

标签: rspec capybara

我有一个规范:

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

并且失败,出现以下错误:

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

但根据我的log/test.log文件,实际上存在重定向。我在这里错过了什么吗?我觉得规范应该过去了。

log/test.log档案:

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

我还尝试在visit '/auth/facebook'行之后添加一个调试器行,然后在规范response.code中返回:成功,所以我明白为什么测试失败,但我不知道不知道当日志清楚地显示重定向时,响应设置为:success的原因。

1 个答案:

答案 0 :(得分:2)

我的猜测是Capybara遵循重定向并获取302响应中的位置指定的页面,这会生成200状态代码。

您可以通过在重定向位置测试页面内容,或使用带有rack-test而非Capybara的控制器规范来处理此问题。