LoginSignup
5
4

More than 5 years have passed since last update.

Rails+Rspec+Capybaraでサブドメイン指定のE2Eテストをする

Last updated at Posted at 2015-03-21

routes.rbに以下のような指定がある場合、サブドメインのURLでアクセスする必要があるが、E2Eテストはどうするのか?

constraints subdomain: 'admin' do
  get 'path/to/article' => 'articles#index'
end

hosts設定

E2Eテストのためにhosts設定しておく

vagrantのゲストOS側にhostsを設定

次のpluginを利用してVagrantfileにhosts設定を記述

adrienthebo/vagrant-hosts

Vagrant.configure(2) do |config|

...

  config.vm.provision :hosts do |provisioner|
    provisioner.add_host '127.0.0.1', ['admin.example.com']
  end

...

end

CircleCI用hosts設定

CircleCI上でのE2Eテストのため、circle.ymlに以下のような設定を記述

machine:
...
  hosts:
    admin.example.com: 127.0.0.1

capybara設定

ホスト名とポート番号を指定

rspecのテストケース等の中で以下のように指定する

# ホスト名にサブドメインを指定
# ポート番号が自動設定されるのでcurrent_sessionから取得
Capybara.app_host = "http://admin.example.com:#{Capybara.current_session.server.port}"

あとは普通にcapybaraでのE2Eテストを書けばよい

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4