テスト用VM(Ubuntu)に apacheやら phpやら入れたものに 更に色々入れます。
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 のインストール
$ rbenv install -l
インストールできるバージョンが表示されるので とりあえず 2.2.2にする
$ 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
いけた!
rubyの使用するバージョンを指定
$ 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
test.php
<?php
$text = htmlspecialchars($_GET["inputtext"], ENT_QUOTES, 'UTF-8');
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
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
test_spec.rb
# encoding: utf-8
require 'spec_helper'
describe '入力フォームページ' do
before do
# 入力画面にアクセスする
visit "/test/test.php"
end
scenario '画面が開いている' do
expect(page).to have_content 'Test Form'
end
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
end
テスト
$ bundle exec rspec spec/test_spec.rb
undefined method `visit' for #<RSpec::ExampleGroups::Nested:0x007f1efc20bc38>
visit が無いって言われてる?
spec_helper.rb に下記を追記
参考: 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.4502 seconds (files took 0.29978 seconds to load)
2 examples, 0 failures
いけました!
日本語入力テストも追加してみる
test_spec.rb
# encoding: utf-8
require 'spec_helper'
describe '入力フォームページ' do
before do
# 入力画面にアクセスする
visit "/test/test.php"
end
scenario '画面が開いている' do
expect(page).to have_content 'Test Form'
end
scenario 'penを入力する' do
#puts source
fill_in 'input_text', with: 'pen'
click_on 'submit_btn'
expect(page).to have_selector '#text_view', text: 'This is a pen.'
end
scenario 'ぺんを入力する' do
#puts source
fill_in 'input_text', with: 'ぺん'
click_on 'submit_btn'
expect(page).to have_selector '#text_view', text: 'This is a ぺん.'
end
end
$ bundle exec rspec spec/test_spec.rb
Finished in 0.72215 seconds (files took 0.3276 seconds to load)
3 examples, 0 failures
すべてOK