I am using rSpec for testing my application. In my application controller I have a method like so:
def set_current_account
@current_account ||= Account.find_by_subdomain(request.subdomains.first)
end
Is it possible to set the request.subdomain in my spec? Maybe in the before block? I am new to rSpec so any advice on this would be great thanks.
Eef
I know this is a relatively old question, but I've found that this depends on what kind of test you're running. I'm also running Rails 4 and RSpec 3.2, so I'm sure some things have changed since this question was asked.
Request Specs
Feature Specs with Capybara
I usually create modules in
spec/support
that look something like this:Include in
spec/rails_helper.rb
:Then you can call within your spec like so:
In rails 3 everything I tried to manually set the host didn't work, but looking the code I noticed how nicely they parsed the path you pass to the request helpers like
get
. Sure enough if your controller goes and fetches the user mentioned in the subdomain and stores it as@king_of_the_castle
Chris Peters' answer worked for me for Request specs, but for Feature specs, I had to make the following changes:
rails_helper:
feature_subdomain_helpers:
I figured out how to sort this issue.
In my before block in my specs I simply added:
This setups up the request.subdomains.first to be the value of the mock_subdomain.
Hope someone finds this useful as its not explained very well anywhere else on the net.