我定义了以下过滤器:
# 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。删除设置 block 会导致我的所有测试失败,因为 find_account 找不到帐户,因此会引发 RecordNotFound
错误。
我做错了什么?
最佳答案
试试这个:
@request.env['HTTP_HOST'] = 'test.myapp.local'
关于ruby-on-rails - 如何在 Rails 测试中伪造子域查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1813038/