LoginSignup
0
0

More than 1 year has passed since last update.

RSpec始めよう!簡単セットアップ

Posted at

1. rspec-railsをbundle install

Gemfile.
group :development, :test do
  gem 'rspec-rails'
end

2. Railsアプリにrspecをインストール

Railsアプリにrspecをインストールする

rspec.
$ bundle exec rails generate rspec:install

RSpecの設定ファイル(.rspec)と、
specフォルダの中にrails_helper.rbspec_helper.rbのファイルがインストールされます。

3. --format documentation追加

必須ではないですが、.rspecファイルに以下の記述を追加することでテスト結果が見やすくなります。

--require spec_helper
--format documentation #追加

4. テストファイル記述

実際に処理を行うファイルを記述します。
require 'rails_helperを記述すること
とりあえずrspecが上手く動作しているかを確認したいのでテストの外枠(itからendまで)だけ作って中身は書いていません

post_spec.rb
require 'rails_helper'

RSpec.describe Post, type: :model do

  it "投稿内容、ユーザーidがあれば有効な状態であること" do
  end
end

bundle exec rspecしてみます。
スクリーンショット 2021-12-14 21.03.51.png

緑の文字で出てきたら成功です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0