views:

223

answers:

1

Hi,

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

+4  A: 

I figured out how to sort this issue.

In my before block in my specs I simply added:

before(:each) do
  @request.host = "#{mock_subdomain}.example.com"
end

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.

Eef
one small tip - request is available as a method as well as an instance variable. It's probably better to access it via the method, to keep a little distance between you and the underlying RSpec code.
pat