【发布时间】:2009-11-28 16:58:08
【问题描述】:
我定义了以下过滤器:
# application_controller.rb
class ApplicationController < ActionController::Base
before_filter :find_account
private
def find_account
@current_account = Account.find_by_subdomain!(request.subdomains.first)
end
end
在我的测试中:
# users_controller_test.rb
class UsersControllerTest < ActionController::TestCase
setup do
@request.host = "test.myapp.local"
end
# ...
end
现在test 被定义为我在所有使用factory_girl 的请求之前加载的虚拟帐户的子域。但是,这是抛出一个 nil 对象错误,说 @request 是 nil。删除 setup 块会导致我的所有测试都失败,因为 find_account 找不到帐户,因此会引发 RecordNotFound 错误。
我做错了什么?
【问题讨论】:
-
我也遇到了同样的问题,你找到解决办法了吗?
-
有用的是发现你正在使用 request.subdomains 来获取子域,我曾经从 params[:subdomain] 获取它但是如果我设置 @request.host 或 @request.env 它不起作用['HTTP_HOST']
标签: ruby-on-rails testing functional-testing