0
1

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 factorybot 動作確認

Posted at

Rspec

Gemfileに以下のコードを追加

Gemfile
group :development, :test do
  gem 'rspec-rails', '~> 4.0.1'
end

ターミナルで

bundle install
rails db:create
bin/rails generate rspec:install

下記のgem追加

Gemfile
group :development do

...元から書いてあるgemは省略

下の1行を追加してください
  gem 'spring-commands-rspec'
end

bundle install実行

bundle install

binstubのgemがインストールできたので、以下のコマンドを実行して新しいbinstubを作成しましょう。

bundle exec spring binstub rspec

コマンドがうまく実行できていれば、アプリケーションのbinディレクトリ内にrspecという実行用ファイルが作成されているはずです。

ターミナルを開いて、以下のコマンドを実行してください。

bin/rspec

すると、以下のような結果が返ってきました。

Finished in 0.03718 seconds (files took 0.78572 seconds to load)
0 examples, 0 failures

まだテストを何も書いていないので、全て0ですが、RSpecはちゃんとインストールされているようです。

これから新しいモデルやコントローラーなどを作成する時に、一緒にRSpecのテストファイルも自動で作成してもらえるように設定しましょう。

config/application.rbを開き、以下のコードを追加します。

config/application.rb
require_relative 'boot'

require 'rails/all'

Bundler.require(*Rails.groups)

module MiyachaApp
  class Application < Rails::Application
    config.load_defaults 6.0


#ここから  
    config.generators do |g|
     g.test_framework :rspec,
       fixtures: false,
       view_specs: false,
       helper_specs: false,
       routing_specs: false
    end
#ここまで

  end
end

Factorybot

Gemfileを開いて、以下の記述を追加してください。

Gemfile
group :development, :test do
#...他のgemは省略
  gem 'factory_bot_rails', '~> 4.10.0'
end

忘れずにbundle install
次にUserモデルを作成

次にターミナルを開いて、以下のコマンドを実行してください。

bin/rails g factory_bot:model user

このコマンドを実行すると、specフォルダ内にfactoriesというフォルダが作成されたはずです。

factoriesフォルダの中のuser.rbファイルを開いて見ましょう。

user.rb
FactoryBot.define do
  factory :user do

  end
end

このfactories/user.rbファイルに、FactoryBotで作成するデータを定義していきます。

それではfactories/user.rbファイルを開いてください。

以下の内容にしてください。

factories/user.rb
FactoryBot.define do
  factory :user do
    email { "test@test.com" } 
    password { "testpassword" }
  end
end

それでは実際にUserモデルを作成して見ましょう。

「spec/models/user_spec.rb」ファイルを開いてください。

このテストファイルでFactoryBotを使ってデータベースを作成しましょう。

user_spec.rbのファイルに以下のexampleを追加してください。

user_spec.rb
require 'rails_helper'

RSpec.describe User, type: :model do
...他のexampleは省略

  it "factorybot test" do
    user = FactoryBot.create(:user)
    expect(user).to be_valid
  end
end

テスト用のテーブル作成

bin/rails db:migrate RAILS_ENV=test

テストの内容としては、

userという関数に、FactoryBotで作成したUserを格納しています。

そして、expectの行で、そのuserがbe_valid(有効である)となるはずであると記述しています。

それでは、テストを実行して見ましょう。

ターミナルを開いて以下のコマンドを実行してください。

bin/rspec spec/models/user_spec.rb

これで実行結果が返ってこれば成功です。

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?