Archive for July, 2011
Rails 3 RSpec Request Spec – Testing Subdomains
How do you test sub-domains in RSpec Request specs (integration tests) ???
# Pass it into the GET request!
get '/programs/100', nil, {'HTTP_HOST' => 'sub.domain.com'}
The 3rd parameter to the get method is a hash of HTTP headers.
See the Rails API documentation for details.
Note:
Depending on the type of test you are working with (support / controller / request / integration etc.) you pass in the sub domain differently.
Here’s some good reference posts on Stack Overflow for setting subdomains in controller specs:
1. Rails RSpec Set Subdomain
# Set the @request.host in a before block
before(:each) do
@request.host = "#{mock_subdomain}.example.com"
end
2. Subdomains in RSpec Controller Tests
# I haven't tried this, and not sure you would need to mock out the current_subdomain method.
@subdomain = 'sub.domain.com'
controller.expects(:current_subdomain).returns(@subdomain)
@request.host = "#{@subdomain}.test.host"
RSpec – Running One Single Test at a Time
In the old days I would pass a regular expression to run a particular unit test or group of similarly named unit tests by name.
Here’s the easy way to run one test in RSpec… by line number!
Look at the line-number of any RSpec block (it, describe, etc), and simply run the rspec command, passing in the [filename]:[line number]:
$ rspec models/user_spec.rb:27
Happy testing!
