How do I set HTTP_REFERER when testing in Rails?

后端 未结 7 1313
我在风中等你
我在风中等你 2020-12-05 22:12

I\'m trying to test a controller and I got this error. I understand the error, but don\'t know how to fix it.

test: on CREATE to :user with completely invali         


        
7条回答
  •  天涯浪人
    2020-12-05 22:55

    In response to the question:

    Why doesn't this work:

    setup { post :create, { :user => { :email => 'invalid@abc' } }, 
    { 'referer' => '/sessions/new' } }
    

    It doesn't work because the Rails doc you linked to documents a different class than the one you're probably using.

    You linked to ActionController::Integration:Session. I'm guessing that you're writing a functional test (if you're using Test::Unit) or a controller test (if you're using Rspec). Either way, you're probably using ActionController::TestCase or a subclass thereof. Which, in turn, includes the module ActionController::TestProcess.

    ActionController::TestProcess provides a get method with different parameters than the get method provided by ActionController::Integration:Session. (Annoying, eh?) The method signature is this:

     def get(action, parameters = nil, session = nil, flash = nil)
    

    Sadly, there is no headers parameter. But at least setting @request.env['HTTP_REFERER'] works.

提交回复
热议问题