この章では、テスト駆動開発について学習。
テスト
Railsのテストには、コントローラーテスト、モデルテスト、統合テストの3種類ある。
ここでは、コントローラーテストについてメモ。
テストの例を2つほど
test "should get home" do
get static_pages_home_url ⇨GETリクエストをhomeアクションに対して発行
assert_response :success ⇨リクエストに対するレスポンスは"成功"になるはず
end
test "should get home" do
get static_pages_home_url
assert_response :success
assert_select "title", "Home | Ruby on Rails Tutorial Sample App" ⇨titleタグ内に「Home | Ruby on Rails Tutorial Sample App」と言う文字列があるはず
end
setupメソッド
書くテストが実行される直前で実行されるメソッド。
provideメソッド
provideメソッドでパラメータを引き渡す
<% provide(:title, "Home") %>
yieldメソッドで受け取る
<title><%= yield(:title) %></title>
テストの便利な設定
テスト用の設定として、minitest reportersとGuardについてメモ。
minitest reporters
テストの結果をプログレスバーでパーセント表示したり、REDやGREENで表示してくれる設定。
minitest-reporters gemを利用。
Gemfile.rb
source 'https://rubygems.org'
.
.
group :test do
gem 'rails-controller-testing', '1.0.2'
gem 'minitest', '5.10.3'
gem 'minitest-reporters', '1.1.14' ⇦これを追加
gem 'guard', '2.13.0'
gem 'guard-minitest', '2.4.4'
end
.
.
test/test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters" ⇦これを追加
Minitest::Reporters.use! ⇦これを追加
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests
# in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
Guardによるテストの自動化
rails testコマンドを手動で打ち込まなくても、static_pages_test.rbファイルなどを変更すると自動的にテストを実行してくれるツール。
Gemfile.rb
source 'https://rubygems.org'
.
.
group :test do
gem 'rails-controller-testing', '1.0.2'
gem 'minitest', '5.10.3'
gem 'minitest-reporters', '1.1.14'
gem 'guard', '2.13.0' ⇦これを追加
gem 'guard-minitest', '2.4.4' ⇦これを追加
end
.
.
初期化
$ bundle exec guard init
Cloud9を使っている場合は、tmuxをインストールする必要がある。
$ sudo yum install -y tmux
Guardfile編集
Guardfile.rb
# Guardのマッチング規則を定義
guard :minitest, spring: "bin/rails test", all_on_start: false do
watch(%r{^test/(.*)/?(.*)_test\.rb$})
watch('test/test_helper.rb') { 'test' }
watch('config/routes.rb') { integration_tests }
watch(%r{^app/models/(.*?)\.rb$}) do |matches|
"test/models/#{matches[1]}_test.rb"
end
watch(%r{^app/controllers/(.*?)_controller\.rb$}) do |matches|
resource_tests(matches[1])
end
watch(%r{^app/views/([^/]*?)/.*\.html\.erb$}) do |matches|
["test/controllers/#{matches[1]}_controller_test.rb"] +
integration_tests(matches[1])
end
watch(%r{^app/helpers/(.*?)_helper\.rb$}) do |matches|
integration_tests(matches[1])
end
watch('app/views/layouts/application.html.erb') do
'test/integration/site_layout_test.rb'
end
watch('app/helpers/sessions_helper.rb') do
integration_tests << 'test/helpers/sessions_helper_test.rb'
end
watch('app/controllers/sessions_controller.rb') do
['test/controllers/sessions_controller_test.rb',
'test/integration/users_login_test.rb']
end
watch('app/controllers/account_activations_controller.rb') do
'test/integration/users_signup_test.rb'
end
watch(%r{app/views/users/*}) do
resource_tests('users') +
['test/integration/microposts_interface_test.rb']
end
end
# 与えられたリソースに対応する統合テストを返す
def integration_tests(resource = :all)
if resource == :all
Dir["test/integration/*"] else
Dir["test/integration/#{resource}_*.rb"]
end
end
# 与えられたリソースに対応するコントローラのテストを返す
def controller_test(resource)
"test/controllers/#{resource}_controller_test.rb"
end
# 与えられたリソースに対応するすべてのテストを返す
def resource_tests(resource)
integration_tests(resource) << controller_test(resource)
end
Guard使用時のSpringとGitの競合を防ぐには、.gitignoreファイルにspring/ディレクトリを追加する。
そうすることで、指定したファイルはGitリポジトリに追加されなくなる。
.gitignore
# See https://help.github.com/articles/ignoring-files for more about
# ignoring files.
#
# If you find yourself ignoring temporary files generated by your
# text editor or operating system, you probably want to add
# a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'
# Ignore bundler config.
/.bundle
# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal
# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep
# Ignore Byebug command history file.
.byebug_history
# Ignore Spring files.
/spring/*.pid ⇦これを追加
設定が完了したので実行
$ bundle exec guard