8

I am developing an application that with rely heavily on subdomains. It has an application embedded in the application (a module) that will be a backend to administer the application. Let's name it kong.

I have this code in my routes file:

constraints :subdomain => "kong" do
  scope :module => "kong", :as => "kong" do
    resources :clients
  end
end

How can I test this route so that when I write something like the following it fetches from the subdomain and only from the subdomain:

get :index
Robert Audi
  • 8,019
  • 9
  • 45
  • 67

1 Answers1

6

In test unit i used something like this to set the request.host to come from a subdomain:

def get_sub(sub = "one")
  @request.host = "#{sub}.local.me" 
end

I personally would put that into the spec_helper.rb file and reference when you need.

For you, in those tests, you're setting sub to equal "kong" probably like

before :each do
  get_sub("kong")
end

This joker also has an answer too, which i found after through google

Community
  • 1
  • 1
pjammer
  • 9,489
  • 5
  • 46
  • 56
  • 7
    Why did you call this method `get_sub` when it's clearly setting a value? Also, why shorten it? `set_subdomain` or `stub_subdomain` would be excellent names for this method. – Ryan Bigg Apr 17 '12 at 14:42
  • 2
    I guess in the perfect hindsight world, i would have called it `set_subdomain` but pedantic aside, the answer still works. the OP can change the method to whatever he chooses. – pjammer Apr 17 '12 at 14:52
  • 5
    note this will not work in routing specs, as `@request` will be nil – lulalala May 14 '12 at 07:26