0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

RSpecの導入と単体テストコード実行の手順

Last updated at Posted at 2020-12-04

Gemfileをインストール

Gemfile
group :development, :test do
   # Call 'byebug' anywhere in the code to stop execution and get a debugger console
   gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
   gem 'rspec-rails', '~> 4.0.0'
 end

bundle install実行

ターミナル
bundle install

RSpecの設定を行う

ターミナル
rails g rspec:install

実行すると、以下のようにディレクトリやファイルが生成されます。

ターミナル
create  .rspec
 create  spec
 create  spec/spec_helper.rb
 create  spec/rails_helper.rb

##.rspecに以下を追加
.rspecというファイルをテキストエディタで開き、以下のように記述します。この記述は、テストコードの結果をターミナル上で可視化するための記述です。

.rspec
--require spec_helper
--format documentation

これでRSpecの設定は完了。

##テストコードを各ファイルの生成

ターミナル
% rails g rspec:model user
spec/models/user_spec.rb
require 'rails_helper'
 RSpec.describe User, type: :model do
   describe 'ユーザー新規登録' do
     it "nicknameが空だと登録できない" do
     end
     it "emailが空では登録できない" do
     end
   end
 end

##テストコード実行

ターミナル
bundle exec rspec spec/models/hoge_spec.rb 

以下のようにテストコードの結果が緑(エディタの設定によって色は異なります。)になっていればテスト終了。
image.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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?