2011-11-24 47 views
1

我有一個規範:日誌顯示 '重定向',但RSpec的看到 '成功'

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

回答

2

我的猜測是,水豚遵循重定向並獲取由302響應中的位置指定的頁面,該頁面生成200狀態碼。

您可以通過測試重定向位置處的頁面內容,或者使用機架測試而不是水豚的控制器規格來處理此問題。

+0

對不起。這原來是問題(和修復)。我來自'test_unit',它沒有自動執行重定向,所以我正在做一個不再是真的假設。在這個問題接受的答案代碼也幫助我得到我需要的東西:http://stackoverflow.com/questions/5228371/how-to-get-current-path-using-capybara – jefflunt

相關問題