このとき 書いてた、「自然言語で書けるE2Eテストを試していってみよう」で試してみたときの環境構築手順。
自分的めもです。
Gitと rbenv インストール
$ sudo apt-get install git rbenv
rbenvのパスを .bashrcに追加
$ vim .bashrc
export PATH="$HOME/.rbenv/bin:$PATH" eval "$(rbenv init -)"
$ source .bashrc
ruby-build を clone
$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
Ruby 2.2.2 のインストール
インストールできるバージョンが表示されるので とりあえず 2.2.2にする
$ rbenv install -l $ rbenv install 2.2.2 BUILD FAILED (Ubuntu 14.04 ・・・・) Try running `apt-get install -y libreadline-dev` to fetch missing dependencies.
エラーが出た…。素直に従う。
$ sudo apt-get install -y libreadline-dev $ rbenv install 2.2.2 Installed ruby-2.2.2
いけた。
インストールした 2.2.2をデフォルトに
$ rbenv versions * system (set by /home/ubuntu/.rbenv/version) 2.2.2 $ rbenv global 2.2.2 system * 2.2.2 (set by /home/ubuntu/.rbenv/version) $ ruby -v ruby 2.2.2
PhantomJSをインストール
$ sudo apt-get install phantomjs
bundlerをインストール
$ gem install bundler $ bundle -v Bundler version 1.10.6
テスト用ディレクトリ作成
$ mkdir test $ cd test
Gemfileを作成して Capybara、Rspec、Poltergeist、諸々必要そうなもの インストール
$ vim Gemfile
source 'https://rubygems.org' gem 'capybara-mechanize' gem 'rspec' gem 'poltergeist'
$ bundle install --path vendor/bundle
テストページ用意
入力した内容が "This is a ○○"で表示される PHPページ
$ vim test.php
<?php $text = $_GET["inputtext"]; if($text) $text = "This is a {$text}."; print <<<EOF <html> <body> <h1>Test Form</h1> <div id="text_view">{$text}</div> <form> <input type="text" name="inputtext" id="input_text"> <input type="submit" id="submit_btn"> </form> </body> </html> EOF; ?>
spec_helper作成
$ vim spec/spec_helper.rb
# encoding: utf-8 require 'capybara/rspec' require 'capybara/poltergeist' Capybara.default_driver = :poltergeist Capybara.app_host = '(ホスト)' Capybara.run_server = false
テストも作る
$ vim spec/test_spec.rb
# encoding: utf-8 require 'spec_helper' describe '入力フォームページ' do # 前提 before do # 入力画面にアクセスする visit "/test/test.php" end # テスト01 入力画面が開いてる scenario '画面が開いている' do expect(page).to have_content 'Test Form' end # テスト02 英字入力 scenario 'penを入力する' do fill_in 'input_text', with: 'pen' click_on 'submit_btn' # This is a pen.って表示される expect(page).to have_selector '#text_view', text: 'This is a pen.' end # テスト03 日本語入力 scenario 'ぺんを入力する' do fill_in 'input_text', with: 'ぺん' click_on 'submit_btn' # This is a ぺん.って表示される expect(page).to have_selector '#text_view', text: 'This is a ぺん.' end end
テスト実行
$ bundle exec rspec spec/test_spec.rb undefined method `visit' for #<RSpec::ExampleGroups::Nested:0x007f1efc20bc38>
visit が無いって言われてる?
参考
http://qiita.com/katryo/items/d001c7058e45506ef1b7
spec_helper.rb の一番下に下記を追加する
RSpec.configure do |config| include Capybara::DSL end
もう一度テスト
$ bundle exec rspec spec/test_spec.rb Finished in 0.72215 seconds (files took 0.3276 seconds to load) 3 examples, 0 failures
テスト3つとも通った。わいわい!