RailsApp Rails RSpec Guard 2018年 11月 13日
前回は RSpec でのテストを作成した. しかし,spec や実装を記述するたびにテストを実行するのは面倒である. そこで,ファイル保存時に自動的にテストを実行してくれる guard を設定する.
group :development do
gem 'guard-rspec', require: false
(中略)
end
bundle
Fetching lumberjack 1.0.13
Fetching coderay 1.1.2
Fetching formatador 0.2.5
Installing formatador 0.2.5
Installing lumberjack 1.0.13
Installing coderay 1.1.2
Fetching nenv 0.3.0
Installing nenv 0.3.0
Fetching shellany 0.0.1
Installing shellany 0.0.1
Fetching guard-compat 1.2.1
Installing guard-compat 1.2.1
Fetching notiffany 0.1.1
Installing notiffany 0.1.1
Fetching rspec 3.8.0
Installing rspec 3.8.0
Fetching pry 0.12.0
Installing pry 0.12.0
Fetching guard 2.14.2
Installing guard 2.14.2
Fetching guard-rspec 4.7.3
Installing guard-rspec 4.7.3
$ be guard init rspec
11:19:26 - INFO - Writing new Guardfile to /Users/hkob/rails/attendance/Guardfile
11:19:26 - INFO - rspec guard added to Guardfile, feel free to edit it
guard :rspec, cmd: "bundle exec rspec" do
$ bin/spring binstub rspec
The 'rspec' command is not known to spring.
group :development do
gem 'spring-commands-rspec'
(中略)
end
Fetching spring-commands-rspec 1.0.4
Installing spring-commands-rspec 1.0.4
$ bin/spring binstub rspec
* bin/rspec: generated with spring
$ bin/rspec
Running via Spring preloader in process 52269
ApplicationHelper
generate date strings
wareki, ewareki wareki_num, wareki_ymd and md should have correct values
Finished in 0.00842 seconds (files took 2.63 seconds to load)
1 example, 0 failures
guard :rspec, cmd: "bin/rspec" do
$ be guard
12:46:26 - INFO - Guard::RSpec is running
12:46:26 - INFO - Guard is now watching at '/Users/hkob/rails/attendance'
/Users/hkob/rails/attendance/vendor/bundle/ruby/2.5.0/gems/guard-2.14.2/lib/guard/jobs/pry_wrapper.rb:279: warning: method Pry#input_array is deprecated. Use Pry#input_ring instead
[1] guard(main)>
RSpec.describe ApplicationHelper, type: :helper do
context 'generate date strings' do
# (ここには前回の spec が書かれている.以下の it を追加)
it 'wareki_by_date, ewareki_by_date wareki_num_by_date, wareki_ymd_by_date and md_by_date should have correct values' do
subject.each do |ymd, jy, ey, y, jm, jd|
date = Date.new(*ymd)
expect(wareki_by_date(date)).to eq jy
expect(ewareki_by_date(date)).to eq ey
expect(wareki_num_by_date(date)).to eq y
jymd = "#{jy}年#{jm}#{jd}"
expect(wareki_ymd_by_date(date)).to eq jymd
expect(md_by_date(date)).to eq "#{jm}#{jd}"
end
end
end
end
12:51:42 - INFO - Running: spec/helpers/application_helper_spec.rb
Running via Spring preloader in process 52389
ApplicationHelper
generate date strings
wareki, ewareki wareki_num, wareki_ymd and md should have correct values
wareki_by_date, ewareki_by_date wareki_num_by_date, wareki_ymd_by_date and md_by_date should have correct values (FAILED - 1)
Failures:
1) ApplicationHelper generate date strings wareki_by_date, ewareki_by_date wareki_num_by_date, wareki_ymd_by_date and md_by_date should have correct values
Failure/Error: expect(wareki_by_date(date)).to eq jy
NoMethodError:
undefined method `wareki_by_date' for #<RSpec::ExampleGroups::ApplicationHelper::GenerateDateStrings:0x00007fa225dd1020>
Did you mean? wareki_ymd
# ./spec/helpers/application_helper_spec.rb:27:in `block (4 levels) in <main>'
# ./spec/helpers/application_helper_spec.rb:25:in `each'
# ./spec/helpers/application_helper_spec.rb:25:in `block (3 levels) in <main>'
# -e:1:in `<main>'
Finished in 0.02964 seconds (files took 2.94 seconds to load)
2 examples, 1 failure
Failed examples:
rspec ./spec/helpers/application_helper_spec.rb:24 # ApplicationHelper generate date strings wareki_by_date, ewareki_by_date wareki_num_by_date, wareki_ymd_by_date and md_by_date should have correct values
/Users/hkob/rails/attendance/vendor/bundle/ruby/2.5.0/gems/guard-2.14.2/lib/guard/jobs/pry_wrapper.rb:279: warning: method Pry#input_array is deprecated. Use Pry#input_ring instead
# @param [Date] d 日付
# @return [String] 和暦年度文字列 (例: 平成21, 平成元, 昭和43).
def wareki_by_date(d)
wareki d.year, d.month, d.day
end
# @param [Date] d 日付
# @return [String] 英文和暦記号年度文字列 (例: H21, H1, S43) .
def ewareki_by_date(d)
ewareki d.year, d.month, d.day
end
# @param [Date] d 日付
# @return [Fixnum] 和暦年度 (21, 1, 43)
def wareki_num_by_date(d)
wareki_num d.year, d.month, d.day
end
# @param [Date] d 日付
# @return [String] 和暦年度月日 (例: 平成21年12月7日, 平成元年1月7日, 昭和43年4月25日)
def wareki_ymd_by_date(d)
wareki_ymd d.year, d.month, d.day
end
# @param [Date] d 日付
# @return [String] 月日 (例: 12月7日, 1月7日, 4月25日)
def md_by_date(d)
md d.month, d.day
end
13:03:42 - INFO - Running: spec/helpers/application_helper_spec.rb
Running via Spring preloader in process 52597
ApplicationHelper
generate date strings
wareki, ewareki wareki_num, wareki_ymd and md should have correct values
wareki_by_date, ewareki_by_date wareki_num_by_date, wareki_ymd_by_date and md_by_date should have correct values
Finished in 0.00708 seconds (files took 1.69 seconds to load)
2 examples, 0 failures
/Users/hkob/rails/attendance/vendor/bundle/ruby/2.5.0/gems/guard-2.14.2/lib/guard/jobs/pry_wrapper.rb:279: warning: method Pry#input_array is deprecated. Use Pry#input_ring instead
長くなったので今日はここまで