リア充爆発日記

You don't even know what ria-ju really is.

rspecでroutingのテスト

本当のリア充はクリスマスだからといってむやみにツイートを抑えたり偽装したりしない。通常運転。

Rails3+rspec2でWeb APIサーバのpostをテストする方法がよくわからなかった件の続編。

今度はAPIサーバ、つまりサブドメイン縛りのリソースに対してroutingのテストをした時のメモ。

routingのテストはspec/routingディレクトリに配置するらしい。
https://github.com/rspec/rspec-rails#routing-specs

で、サブドメイン縛りっていうのがどうやったらいいのかよくわからなくて試行錯誤した結果が結局これ。

profiles_route_spec.rb
require 'spec_helper'

describe "routing to posts" do

  it "routes to #show" do
    expect(get: "http://api.example.com/v1/users/1/profiles")
    .to route_to(action: "show" ,controller: "v1/profiles", user_id: "1")
  end

  it "routes to #create" do
    expect(post: "http://api.example.com/v1/users/1/profiles")
    .to route_to(action: "create" ,controller: "v1/profiles", user_id: "1")
  end

end

##########

routes.rb
~snip~
  constraints subdomain: 'api' do
    namespace :v1 do
      resources :users do
        resource :profiles
      end
    end
  end

もう、FQDNがっつり書くっていう。
ちなみにこれ、has_oneの関係になっているケースなのでresources :users doに対してresource(resourcesではない)をネストしている。

メリークリスマス!