testing routes with subdomain constraints using rspec

◇◆丶佛笑我妖孽 提交于 2020-01-29 04:54:20

问题


I'm having trouble getting my rspec routing tests working with a subdomain constraint.

Specifically I have a route

constraints :subdomain => "api" do
  resources :sign_ups, :only => [:create]
end

and (among others) a test

it "does allow creation of sign ups" do
  {:post => "/sign_ups"}.should route_to(
    :controller => "sign_ups",
    :action => "create",
  )
end

If I remove the subdomain constraint this test passes, but with it it fails. I have to tell rspec to use the subdomain but I'm at a loss as to how

TIA

Andy


回答1:


I usually do:

let(:url)     { "http://api.domain.com"     }
let(:bad_url) { "http://bad_url.domain.com" }

it "does allow creation of sign ups" do
  {:post => "#{url}/sign_ups"}.should route_to(
   :controller => "sign_ups",
   :action => "create",
  )
end

it "should not route" do
  {:post => "#{bad_url}/sign_ups"}.should_not route_to(
   :controller => "sign_ups",
   :action => "create",
  )
end


来源:https://stackoverflow.com/questions/9329669/testing-routes-with-subdomain-constraints-using-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!