RSpecをインストールしてみる。
前回はRailsでモデル同士の関連付けをやりましたので、今回はRSpecでもインストールしてみますー。
Gemfile
・・・
gem ‘rspec-rails’
・・・
Gemfile
に上記記述を足しまして、
$ bundle install
# 設定ファイルを作成
$ bundle exec rails g rspec:install
# rspecコマンドを実行
$ bundle exec rspec
これで準備はOK。
rspecコマンドが使えるようになっているはずです。
早速、モデルのテストをすべく準備をしましょう。
$ bundle exec rails g rspec:model post
create spec/models/post_spec.rb
上記コマンドで、Post
モデルのテスト用ファイルが作成されます。
早速、spec/models/post_spec.rb
を見てみると・・・
post_spec.rb
require 'rails_helper'
RSpec.describe Post, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
デフォルトの記述が書かれてますね。
これでテストを試してみます。
$ bundle exec rspec spec/models/post_spec.rb
*
Pending: (Failures listed here are expected and do not affect your suite's status)
1) Post add some examples to (or delete) /User/username/work/and0o0_bbs/spec/models/post_spec.rb
# Not yet implemented
# ./spec/models/post_spec.rb:4
Finished in 0.00072 seconds (files took 1.92 seconds to load)
1 example, 0 failures, 1 pending
1件がペンディング(保留)中だという結果ですね。
とりあえず今回はここまで。
次回は、モデルのテストをどんどん書いていきたいと思います。