【发布时间】:2011-03-05 02:23:27
【问题描述】:
我已经编写了一个功能测试,它改变了请求对象的一些环境变量来模拟用户已经登录。
require 'test_helper'
class BeesControllerTest < ActionController::TestCase
# See that the index page gets called correctly.
def test_get_index
@request.env['HTTPS'] = "on"
@request.env['SERVER_NAME'] = "sandbox.example.com"
@request.env['REMOTE_USER'] = "joeuser" # Authn/Authz done via REMOTE_USER
get :index
assert_response :success
assert_not_nil(assigns(:bees))
assert_select "title", "Bees and Honey"
end
end
功能测试工作正常。
现在我想做一些类似的事情作为集成测试的一部分。这是我尝试过的:
require 'test_helper'
class CreateBeeTest < ActionController::IntegrationTest
fixtures :bees
def test_create
@request.env['HTTPS'] = "on"
@request.env['SERVER_NAME'] = "sandbox.example.com"
@request.env['REMOTE_USER'] = "joeuser" # Authn/Authz done via REMOTE_USER
https?
get "/"
assert_response :success
[... more ...]
end
end
我收到一条错误消息,抱怨 @request 为零。我怀疑这与会话对象有关,但我不确定如何让它工作。
【问题讨论】:
-
@request.env['HTTPS'] = "on"行抱怨您不能将env方法应用于 nil 值。 -
这是一个三年前提交给 Rails 的补丁,解决了这个问题:dev.rubyonrails.org/ticket/8209
标签: ruby-on-rails environment-variables integration-testing