Rails が 4.2 がリリースされた。作成しているシステムも 4.2 ベースにアップグレードするが、溜め込んでいるネタを吐き出してからにする。
Guard のインストール
ローカルでの自動テストを実施するために、guard をインストールする。また、受入テストのために turnip もインストールしておく。
group :development, :test do (省略) gem 'guard-rspec’, '~> 2.10.4' end group :test do (省略) gem 'turnip', '~> 1.2.4' end
bundle install する。
$ bundle install Installing hitimes 1.2.2 Installing timers 4.0.1 Installing celluloid 0.16.0 Installing coderay 1.1.0 Installing formatador 0.2.5 Installing gherkin 2.12.2 Installing rb-fsevent 0.9.4 Installing rb-inotify 0.9.5 Installing listen 2.8.4 Installing lumberjack 1.0.9 Installing nenv 0.1.1 Installing method_source 0.8.2 Installing slop 3.6.0 Installing pry 0.10.1 Installing guard 2.10.4 Installing guard-compat 1.1.0 Installing rspec 3.1.0 Installing guard-rspec 4.5.0 Installing turnip 1.2.4
bundle exec するのが面倒なので、binstubs で bin/guard を作成しておき、それを使って設定ファイルを作成する。
$ bundle binstubs guard $ bin/guard init
guard で起動する rspec は spring の配下にしておく。
guard :rspec, cmd: "bin/spring rspec" do
設定された guard には FactoryGirl の設定がないので、以下の文を Guardfile を追加する。
require 'active_support/inflector' (中略) # FactoryGirl watch(%r{^spec/factories/(.+)\.rb$}) do |m| [ rspec.spec.("models/#{m[1].singularize}"), rspec.spec.("controllers/#{m[1]}_controller") ] end
ターミナルを一つ用意して、以下のコマンドを実行しテストを起動しておく。
$ bin/guard 08:07:57 - INFO - Guard is using TerminalTitle to send notifications. 08:07:57 - INFO - Guard::RSpec is running 08:07:58 - INFO - Guard is now watching at '/Users/hkob/rails/webcit3' [1] guard(main)>
モデルの追加 (自動テストの確認)
テストの自動化を確認するために、ひとまずモデルを一つ書いてみる。feature/configure_systems ブランチと develop ブランチが同一になっているので、取り急ぎ feature/create_models ブランチを作ってみる。
最初に作成するモデルは年度を保存する Year である。属性は year (integer)、default_year (boolean) の二つだけである。両属性とも null 禁止である。feature/create_models に切り替え、以下のコマンドでモデルを作成する。
$ bin/rails g model year year:integer default_year:boolean
invoke active_record
create db/migrate/20141220110018_create_years.rb
create app/models/year.rb
invoke rspec
create spec/models/year_spec.rb
invoke factory_girl
create spec/factories/years.rb
ちゃんと factoryGirl の spec も作成されていることがわかる。
さっそくテーブルを作成するが、データベース側にも null 禁止を設定しておく。また、year 属性にはインデックスを付け、 unique 属性を設定しておく。db/migrate/*_create_years.rb は以下のようになる。
class CreateYears < ActiveRecord::Migration def change create_table :years do |t| t.integer :year, null:false t.boolean :default_year, null:false t.timestamps end add_index :years, :year, unique:true end end
データベースを作成し、上記のテーブルを作成する。
$ bin/rake db:create $ bin/rake db:migrate == 20141220110018 CreateYears: migrating ====================================== -- create_table(:years) -> 0.0106s -- add_index(:years, :year, {:unique=>true}) -> 0.0076s == 20141220110018 CreateYears: migrated (0.0185s) =============================
試しにこの段階でテストを実行してみる。
$ bin/rspec Year add some examples to (or delete) /Users/hkob/rails/webcit3/spec/models/year_spec.rb (PENDING: Not yet implemented) Pending: Year add some examples to (or delete) /Users/hkob/rails/webcit3/spec/models/year_spec.rb # Not yet implemented # ./spec/models/year_spec.rb:4 Finished in 0.00045 seconds (files took 0.91649 seconds to load) 1 example, 0 failures, 1 pending
テストが書かれておらず、Pending になっている旨が表示される。しかしながら、毎回実行するのは面倒なので、先ほど別のターミナルで実行していた Guard を用いる。しかしながら、ファイルを追加しただけでは Guard は動作していない。そこで、guard が動作しているターミナルで、リターンキーを押してみる。
[1] guard(main)> 08:19:46 - INFO - Run all 08:19:46 - INFO - Running all specs Year add some examples to (or delete) /Users/hkob/rails/webcit3/spec/models/year_spec.rb (PENDING: Not yet implemented) Pending: Year add some examples to (or delete) /Users/hkob/rails/webcit3/spec/models/year_spec.rb # Not yet implemented # ./spec/models/year_spec.rb:4 Finished in 0.00043 seconds (files took 1.75 seconds to load) 1 example, 0 failures, 1 pending
先ほどと同じ結果が出ていることがわかる。これで year 関係のファイルが guard に補足された。例えば、year_spec.rb のファイルの時刻を更新してみる。
$ touch spec/models/year_spec.rb
この結果、再度テストが実行された。次は実際にテストを記述してみる。
今日はここまで
written by iHatenaSync