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. Remov开发者_运维百科ing 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?
Try this:
@request.env['HTTP_HOST'] = 'test.myapp.local'
精彩评论