【问题标题】:RSpec request object is nilRSpec 请求对象为零
【发布时间】:2012-11-02 03:24:56
【问题描述】:

我正在尝试在我的请求规范中测试 cookie:

require 'spec_helper'

describe "Cookies"
  it "should set correctly" do
    request.cookies['foo'] = 'bar'
  end
end

但这给了我undefined method 'cookies' for nil:NilClass。我该如何解决这个问题?

【问题讨论】:

    标签: ruby-on-rails cookies rspec capybara


    【解决方案1】:

    在您使用 getpostdeleteput 等方法之一发出 http 请求之前,您的测试用例中的 request 对象将为零。

    例如,如果您在 request.cookies['foo'] = 'bar' 前面加上 get root_path,您的代码就可以工作。

    【讨论】:

    • 如果我想在发出请求之前设置request.env['HTTPS']标头怎么办?
    • 对于 https 请求,您可以在发出请求之前使用 rspec https! 助手。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    相关资源
    最近更新 更多