RSpec请求对象为零

Eri*_*ang 4 cookies rspec ruby-on-rails capybara

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

require 'spec_helper'

describe "Cookies"
  it "should set correctly" do
    request.cookies['foo'] = 'bar'
  end
end
Run Code Online (Sandbox Code Playgroud)

但这给了我undefined method 'cookies' for nil:NilClass.我该如何解决?

cde*_*ers 7

request对象将是你的测试用例中零,直到您使用的方法之一的HTTP请求get,post,delete,put,等.

例如,如果您之前request.cookies['foo'] = 'bar'有代码,则代码将起作用get root_path.

  • 如果我想在*发出请求之前设置`request.env ['HTTPS']`标头*怎么办? (4认同)