RSpecの基本
基本構造
RSpec.describe "説明の対象を記述" , type: :対象のタイプ do it "何を説明するか" do "テスト" end end
Userモデルを説明の対象とする場合
#spec/models/user_spec.rb RSpec.describe "User" , type: :model do it "何を説明するか" do end end
describeはネストも可能であるためグループを分けられる、Userモデルのcreateアクションとfindアクションを対象とする場合
#spec/models/user_spec.rb RSpec.describe "User" , type: :model do describe "#create" do it "何を説明するか" do end end describe "#find" do it "何を説明するか" do end end end
contextと条件を分ける。Userの結合テストの場合
RSpec.describe 'Users', type: :system do context 'ユーザー新規登録ができるとき' do end context 'ユーザー新規登録ができないとき' do end end
RSpec公式サイト
github.com