简体   繁体   English

从功能规范中发现难以模拟 request.referrer

[英]finding difficulty mocking request.referrer from feature specs

From my rspec FEATURE test I want to set request.referrer to '/pagea'.从我的 rspec FEATURE 测试中,我想将 request.referrer 设置为“/pagea”。

Following is my test:以下是我的测试:

controller.request.should_receive(:referrer).and_return('/pagea')
visit '/pageb'

expect(page).to have_content 'xyz'

This is the error I face:这是我面临的错误:

undefined local variable or method `controller' for #<RSpec::ExampleGroups

Then when I change the line to然后当我将线路更改为

AbcController.request.should_receive(:referrer).and_return('/pagea')

I get the following error:我收到以下错误:

undefined local variable or method `request' for #<RSpec::ExampleGroups

The source code is as follows:源代码如下:

class AbcController < ApplicationController
  def index
     set_defaults
  end

  def set_defaults
   if request.referrer.present?
     check_user
   end
  end

  def check_user
    if request.referrer.include? "/pagea"
      @field = true
    end
  end

end

Could you help me figure out how to mock the request.referrer.你能帮我弄清楚如何模拟 request.referrer。

我认为你可以这样做

allow_any_instance_of(ActionDispatch::Request).to receive(:referrer).and_return('/pagea')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM