LoginSignup
15
16

More than 5 years have passed since last update.

rspecとfactorybotを導入するときにハマった時に確認したこと

Last updated at Posted at 2018-05-20

railsでrspecとfactorybotを動かそうとしてハマったので確認したことをメモ

最終的には以下のサイトを参考にさせていただきうまくいきました
https://qiita.com/Ushinji/items/522ed01c9c14b680222c

うまくいかないときにやったこと

ターミナルから実行してみる(IDEから試している場合)

IDEを使っている場合は、ターミナルから実行した場合も同様の結果か確認する。
RubyMineやその他のIDEを使っている場合にハマりやすいのが、IDEの方の設定に引きずられてエラーになっているだけというパターン。

IEDだけエラーする場合は、IDEの設定を確認する。パスや実行方法がminitestになっていないかなど。

rspec
bundle exec rspec ./spec/controllers/test_spec.rb

実行するテストファイルの名前は**_spec.rbになっているか

読み込みに失敗する例:test_rspec.rb
test_spec.rbなどのように**_spec.rbとファイル名がなっているか確認する

factory botでのエラー

Factory already registered: xxx

  • 同じfacotryが二つある可能性がある。
    factory xxxで検索して同一のものがないか確認しあれば削除か名前を変更。

  • 設定がおかしくてロードが2回走る?
    初めに設定を行った際、以下ファイルを作成していたところ、一つしか定義していないfactoryに対して、
    factory already registerdのエラーがconfig.before(:suite)の箇所で出ていた。
    rails_helperで config.include FactoryBot::Syntax::Methods を指定しているので、これがなくてもいいと思い、
    思い切ってファイル(spec/support/factory_bot.rb)を削除することで自分の場合は一旦解決。
    ファイルはgithubのfactorybotにあった。
    https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#rspec

factory_bot.rb
# spec/support/factory_bot.rb
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end

# RSpec without Rails
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods

  config.before(:suite) do
    FactoryBot.find_definitions
  end
end

その他のエラー

  • rails c で試して動くか確認してみる
console
rails c
FactoryBot.build(:user)
  • springのせいで動かない可能性?

springが起動しているか確認

terminal
spring status
Spring is not running. # この場合は起動していないのでこの対処は不要なはず

ググってたときにspringのせいで動かない可能性もあるとの書き込みをいくつか見つけた
https://masawada.hatenablog.jp/entry/2018/01/06/012845

上記の手順をやっても解決できないとき

rspceが動作するときどのファイルを使用して、どういう順序で動いているか整理して、原因の切り分けをする。
以下にまとめてみました。
以下を参考にしてbundle exec rails generate rspec:installからするのも検討してみると良いかもです。

rspecを動作させる際に必要なファイル

.rspec
spec
|------spec_helper.rb
|------rails_helper.rb (テストファイルでrequireが必要)
|
|------factories
|     |------user.rb(モデルのテスト用ファイル)
|
|------xxxxx(例. controllers)
     |------test_spec.rb(テストロジックを記載したファイル)

上記ファイルの実行順序

以下コマンドを実行した場合
bundle exec rspec ./spec/xxxxx/test_spec.rb

1. .rspecが読み込まれる

".rspec"
--require spec_helper

2. .rspecでrequireされたspec_helperが呼ばれる

3. spec/xxxxx/test_spec.rbが呼ばれる

test_spec.rb
require 'rails_helper'

describe 'test' do
  let!(:test) { build(:user) }
  it 'should do something' do
   p test
  end
end

4. spec/rails_helperが呼ばれる
5. spec/factories/user.rbが呼ばれる

user.rb
FactoryBot.define do
  factory :user do
    email "bbb@com"
    created_at Time.now
    updated_at Time.now
  end
end
結果
bundle exec rspec spec/controllers/show_list_spec.rb 

#<User id: nil, email: "bbb@com", created_at: "2018-05-20 14:34:28", updated_at: "2018-05-20 14:34:28">
.

Finished in 0.04037 seconds (files took 3.17 seconds to load)
1 example, 0 failures
15
16
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
15
16