参考資料
公式サイト
github.com
RSpecの実行ログのフォーマット設定
shinkufencer.hateblo.jp
↑
ありがとうございます! 美しくログが表示されました🙇🏻♀️
導入
Gemfileに記述
# 最新の安定版リリースに対して実行する
group :development, :test do
gem 'rspec-rails', '~> 4.0', '>= 4.0.1'
end
特定バージョンを指定する場合
下記で記述方法を確認する。他のGemとの依存関係にも注意する。
https://rubygems.org/gems/rspec-rails
ダウンロードとインストール
$ cd [プロジェクトdir]
$ bundle install
ボイラープレート構成ファイルを生成する
$ rails generate rspec:install
create .rspec
create spec
create spec/spec_helper.rb
create spec/rails_helper.rb
メンテナンス/バージョンアップ
$ bundle update rspec-rails
specファイルの生成
組込ジェネレーターにフックして、specファイルを同時に作成する場合
$ rails generate model user name:string mail:string
Running via Spring preloader in process 3594
invoke active_record
create db/migrate/20200522093439_create_users.rb
create app/models/user.rb
invoke rspec
create spec/models/user_spec.rb
RSpec独自のスペックファイルジェネレーターを使用して、specファイルのみ作成する場合
$ rails generate rspec:model color name:string hex_color_code:string
Running via Spring preloader in process 3871
create spec/models/color_spec.rb
すべてのRSpecジェネレータの表示方法
$ rails generate --help | grep rspec
Running via Spring preloader in process 3903
rspec:channel
rspec:controller
rspec:feature
rspec:generator
rspec:helper
rspec:install
rspec:integration
rspec:job
rspec:mailbox
rspec:mailer
rspec:model
rspec:request
rspec:scaffold
rspec:system
rspec:view
すべてのテストファイル(*_spec.rb)を実行する
$ bundle exec rspec
特定ディレクトリ内のテストファイルをすべて実行する
$ bundle exec rspec spec/models
特定のテストファイルのみを実行する
$ bundle exec rspec spec/models/user_spec.rb
特定のテストファイルの特定行のみを実行する
$ bundle exec rspec spec/models/user_spec.rb:4
RSpec実行ログを美しく見せる
.rspecに下記を追記
--format documentation
上記設定後に実行
$ bundle exec rspec spec/sample/matcher_spec.rb
matcher
等価
eqの使用
内容が同一の文字列の比較
is expected to eq "kuro-neko"
内容が異なる文字列の比較
is expected not to eq "shiro-neko"
Finished in 0.05316 seconds (files took 5.55 seconds to load)
2 examples, 0 failures
RSpec実行時に使えるすべてのオプションを表示する
$ bundle exec rspec --help
Usage: rspec [options] [files or directories]
-I PATH Specify PATH to add to $LOAD_PATH (may be used more than once).
-r, --require PATH Require a file.
-O, --options PATH Specify the path to a custom options file.
--order TYPE[:SEED] Run examples by the specified order type.
[defined] examples and groups are run in the order they are defined
[rand] randomize the order of groups and examples
[random] alias for rand
[random:SEED] e.g. --order random:123
--seed SEED Equivalent of --order rand:SEED.
--bisect[=verbose] Repeatedly runs the suite in order to isolate the failures to the
smallest reproducible case.
--[no-]fail-fast[=COUNT] Abort the run after a certain number of failures (1 by default).
--failure-exit-code CODE Override the exit code used when there are failing specs.
-X, --[no-]drb Run examples via DRb.
--drb-port PORT Port to connect to the DRb server.
**** Output ****
-f, --format FORMATTER Choose a formatter.
[p]rogress (default - dots)
[d]ocumentation (group and example names)
[h]tml
[j]son
[f]ailures ("file:line:reason", suitable for editors integration)
custom formatter class name
-o, --out FILE Write output to a file instead of $stdout. This option applies
to the previously specified --format, or the default format
if no format is specified.
--deprecation-out FILE Write deprecation warnings to a file instead of $stderr.
-b, --backtrace Enable full backtrace.
--force-color, --force-colour Force the output to be in color, even if the output is not a TTY
--no-color, --no-colour Force the output to not be in color, even if the output is a TTY
-p, --[no-]profile [COUNT] Enable profiling of examples and list the slowest examples (default: 10).
--dry-run Print the formatter output of your suite without
running any examples or hooks
-w, --warnings Enable ruby warnings
**** Filtering/tags ****
In addition to the following options for selecting specific files, groups, or
examples, you can select individual examples by appending the line number(s) to
the filename:
rspec path/to/a_spec.rb:37:87
You can also pass example ids enclosed in square brackets:
rspec path/to/a_spec.rb[1:5,1:6] # run the 5th and 6th examples/groups defined in the 1st group
--only-failures Filter to just the examples that failed the last time they ran.
-n, --next-failure Apply `--only-failures` and abort after one failure.
(Equivalent to `--only-failures --fail-fast --order defined`)
-P, --pattern PATTERN Load files matching pattern (default: "spec/**/*_spec.rb").
--exclude-pattern PATTERN Load files except those matching pattern. Opposite effect of --pattern.
-e, --example STRING Run examples whose full nested names include STRING (may be
used more than once)
-E, --example-matches REGEX Run examples whose full nested names match REGEX (may be
used more than once)
-t, --tag TAG[:VALUE] Run examples with the specified tag, or exclude examples
by adding ~ before the tag.
- e.g. ~slow
- TAG is always converted to a symbol
--default-path PATH Set the default path where RSpec looks for examples (can
be a path to a file or a directory).
**** Utility ****
--init Initialize your project with RSpec.
-v, --version Display the version.
-h, --help You're looking at it.