I'm currently working on a Ruby on Rails project where sub-domains come into play pretty heavily. Each account created on the site has its own sub-domain, which means that there is no pre-set list. I ran into some problems when writing functional tests and integration tests with Capybara, as I had no idea how to set the current domain and assert a change in sub-domain. Fortunately, after struggling through for a couple of hours, I found the solution and I'm putting it here for the benefit of anyone else!
Setting the domain in functional tests
To set the domain in test-unit tests, use the @request.host variable inside the test method:
I was doing this on an account basis, and quite regularly, so I created a helper method to set the domain using the account, and monkey-patched the ActionController::TestCase class:
Setting the domain in integration tests with Capybara
If you aren't using Capybara, you can simply set the current host in integration tests with the host! method:
However, if using Capybara, you also need to set the host (including protocol, http://) with:
Again, I monkey-patched ActionDispatch::IntegrationTest to provide a helper method:
Testing domain changes in integration tests
For non-Capybara tests, you can simply use assert_redirected_to:
When using Capybara, you have access to current_url: