6

次のフィルターを定義しています。

# 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

Nowtestは、を使用するすべてのリクエストの前にロードするダミーアカウントのサブドメインとして定義されていますfactory_girl。ただし、これは、@ requestがnilであるという、nilオブジェクトエラーをスローしています。セットアップブロックを削除すると、find_accountがアカウントを見つけることができず、RecordNotFoundエラーがスローされるため、すべてのテストが失敗します。

私は何が間違っているのですか?

4

1 に答える 1

4

これを試して:

@request.env['HTTP_HOST'] = 'test.myapp.local'
于 2011-01-03T21:02:20.777 に答える