LoginSignup
0
0

More than 1 year has passed since last update.

【RSpec】基本的なRSpec導入手順

Posted at

はじめに

超初歩的なRSpec導入部分のみです
実践はしてませんのでご注意ください

環境

Rails: 6.1.4
Ruby: 2.6.3
RSpec: 3.10
rspec-rails: 5.0.1

手順

1.Gemfileに記述

Gemfile.rb
:
:
  group :test do
    gem 'capybara'
    gem 'rspec-rails'
    gem "factory_bot_rails"
    gem 'faker'
  end
:
end
terminal
bundle install

2.↓必要なファイルをインストールする

terminal
rails g rspec:install

3.specフォルダにfactoriesフォルダを作成
         ↓
factoriesフォルダの中にモデル名.rbを作成
例)book.rbやarticle.rbなど

4.テストデータを定義する

spec/factories/book.rb
FactoryBot.define do
  factory :book do
    title { Faker::Lorem.characters(number:10) }
    body { Faker::Lorem.characters(number:30) }
  end
end

Faker::Loremはgem 'faker'を入れると使えます。
勝手にダミーサンプルを作ってくれるのでいちいちサンプル作らなくて済んで楽です。

上記の場合は
title にランダムな5文字の文字列
contentにランダムな10文字の文字列
を作ってくれます。


5.FactoryBotが使えるように
spec/rails_helper.rbのend直前に記載

spec/rails_helper.rb
:
:
  config.include FactoryBot::Syntax::Methods
end


初歩的な初歩ですがRSpecを使うまでの手順でした!
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