2016-03-17 67 views
0

我試圖運行對成功完成表單一redirect_to(:back)測試,當我跑這條線我得到這個錯誤測試redirect_to的(:回)從成功完成不工作

assert_difference 'Comment.count', 1 do 
     post comments_path, comment: { body: "OPTC", commentable_id: 1, commentable_type: 'Post'} 
end 

錯誤 ActionController::RedirectBackError: No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify request.env["HTTP_REFERER"].

Looking into this我看到我需要這樣的東西添加到安裝request.env['HTTP_REFERER'] = 'http://test.com/'。但這只是給出了錯誤NoMethodError: undefined method 'env' for nil:NilClass

現在我不知道該怎麼做。我怎樣才能讓第一個錯誤消失?

更新

im使用導軌4

這裏是測試文件的

需要 'test_helper'

class CategoryItemShowPageTest < ActionDispatch::IntegrationTest 

def setup 
    @user = users(:michael) 
    @user1 = users(:archer) 
    @guide = Guide.find(1) 
    @category = Category.find(1) 
    @category_item = CategoryItem.find(1) 
    @mod_relationship = game_mods_relationships(:mod1) 
    request.env['HTTP_REFERER'] = 'http://test.com/' 

    end 
+0

可能重複[在Rails中測試時如何設置HTTP \ _REFERER?](http://stackoverflow.com/questions/542856/how-do-i-set-http-referer-when- testing-in-rails) – BoraMa

+0

@BoraMa我讀過這個問題,並嘗試給出的答案,它給了我在我的問題中提出的第二個錯誤 – Rob

+0

您是否嘗試過來自上述鏈接問題的其他答案的解決方案變體?值得注意的是設置'@ request.env'(而不是'request.env')或傳遞'HTTP_REFERER'作爲第二個參數到'post',如[這個答案]所示(http://stackoverflow.com/a/7275895/1544012)? – BoraMa

回答

0

作爲評價上面討論的標頭,我相信,這應該適用於在Rails 4集成測試中設置params和HTTP referer:

post comments_path, 
    { comment: { body: "OPTC", commentable_id: 1, commentable_type: 'Post' } }, 
    { 'HTTP_REFERER' => 'http://www.somewhere.net' } 
+0

謝謝你做到了 – Rob