How do I change the default "www.example.com" domain for testing in rails?

@request.host = 'user.myapp.com'

  • Integration/Request Specs (inheriting from ActionDispatch::IntegrationTest):

     host! 'my.awesome.host'
    

See the docs, section 5.1 Helpers Available for Integration Tests.

alternatively, configure it globally for request specs at spec_helper.rb level:

RSpec.configure do |config|
  config.before(:each, type: :request) do
    host! 'my.awesome.host'
  end
end
  • Controller Specs (inheriting from ActionController::TestCase)

     @request.host = 'my.awesome.host'
    

See the docs, section 4.4 Instance Variables Available.

  • Feature Specs (through Capybara)

     Capybara.default_host = 'http://my.awesome.host'
     # Or to configure domain for route helpers:
     default_url_options[:host] = 'my.awesome.host'
    

From @AminAriana's answer

  • View Specs (inheriting from ActionView::TestCase)

     @request.host = 'my.awesome.host'
    

...or through RSpec:

    controller.request.host = 'my.awesome.host'

See the rspec-rails view spec docs.