我定义了以下过滤器:

# 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 在所有请求之前加载的虚拟帐户的子域。但是,这是抛出一个零对象错误,说@request是零。删除设置块会导致我的所有测试失败,因为find_account找不到帐户,因此会抛出 RecordNotFound 错误。

我做错了什么?

有帮助吗?

解决方案

试试这个:

@request.env['HTTP_HOST'] = 'test.myapp.local'
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top