Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fake out a subdomain lookup in Rails tests?

I have the following filter defined:

# 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

and in my test:

# users_controller_test.rb
class UsersControllerTest < ActionController::TestCase
  setup do
    @request.host = "test.myapp.local"
  end
  # ...
end

Now test is defined as the subdomain for a dummy account that I load prior to all requests using factory_girl. However, this is throwing a nil object error, saying that @request is nil. Removing the setup block causes all of my tests to fail as find_account cannot find an account and therefore throws a RecordNotFound error.

What am I doing wrong?

like image 263
Wayne Molina Avatar asked Nov 28 '09 16:11

Wayne Molina


1 Answers

Try this:

@request.env['HTTP_HOST'] = 'test.myapp.local'
like image 77
Avand Amiri Avatar answered Nov 07 '22 09:11

Avand Amiri