在完成成功后测试redirect_to(:back)不起作用

时间:2016-03-17 08:23:50

标签: ruby-on-rails ruby-on-rails-4 minitest

我正在尝试运行一个成功完成表单redirect_to(:back)的测试,运行此行时出现此错误

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

现在我不知道该怎么做。如何让第一个错误消失?

更新

我正在使用rails 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

1 个答案:

答案 0 :(得分:0)

正如上面的评论中所讨论的,我相信,在Rails 4集成测试中设置params和HTTP referer应该可以正常工作:

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