0
0

【Rails】RSpec便利なカスタマイズ

Last updated at Posted at 2024-05-08

カスタマイズ一覧

spec_helper.rb
RSpec.configure do |config|
  # 1
  config.filter_run_when_matching :focus

  # 2
  config.example_status_persistence_file_path = "spec/examples.txt"

  # 3
  config.disable_monkey_patching!

  # 4
  if config.files_to_run.one?
    config.default_formatter = "doc"
  end

  # 5
  config.profile_examples = 10

  # 6
  config.order = :random

  # 7
  Kernel.srand config.seed
end

1. 特定のテストのみ実行する方法

方法1

// rspec ファイルパス:行数
$ rspec spec/models/user_spec.rb:12

ファイル名と行数を指定すると,テストを限定して実行することができます.
しかし行数を確認する作業や,複数ファイルに跨ったテストは大変です……

方法2

そこで以下の設定をします.

spec_helper.rb
RSpec.configure do |config|
  config.filter_run_when_matching :focus
end

テストブロックの先頭にfをつけることで実行するテストを指定することができます.

e.g.: fdescribe, fcontext, fit

user_spec.rb
RSpec.describe User, type: :model do
  describe 'validations' do
    it 'is valid with valid attributes' do
        # ...
    end

    # このテストのみ実行される!
    fit 'is not valid without a user_name' do 
        # ...
    end

    it 'is not valid without an email' do
        # ...
    end
  end
end

commitする時fの消し忘れに注意してください!

方法3

$ rspec --example '実行したいexampleやdescribeの名前の一部'

e.g.:

# describeやexampleにtestが含まれているものが実行される
# (SQLで言うところの LIKE '%test%')
$ rspec --example 'test'

詳細はこちらを参照

2. 前回失敗したテストのみ実行する方法

$ rspec --only-failures

これを実行するには以下の設定が必要です.

spec_helper.rb
RSpec.configure do |config|
  config.example_status_persistence_file_path = "spec/examples.txt"
end

仕組みとしてはRSpec実行後,spec/examples.txtにテスト結果を一時保管しておくことで,失敗したテストだけを実行できるわけです.

ちなみに以下のコマンドは--only-failuresを適用し,1回失敗したら中断するコマンドです.

$ rspec --next-failure

3. モンキーパッチを無効にする

モンキーパッチとは?

モンキーパッチは、プログラミングにおいて既存のコードを変更する技術です。Rubyでは、既存のクラスやモジュールに新しいメソッドを追加したり、既存のメソッドを上書きしたりすることが可能です。これにより、アプリケーションの特定のニーズに合わせてカスタマイズできます。
参考

新しいメソッドを追加したり,既存メソッドのバグ修正に役立ちますが,他への影響や可読性が低下する等のデメリットもあるそうです.

spec_helper.rb
RSpec.configure do |config|
  config.disable_monkey_patching!
end

##追記

monkey_patchを無効にするとテストコードの1行目にRSpec.describeと書く必要があります.
テストコードはRSpecを明示的に書くことを推奨してるそうです.
つまり,disable_monkey_patchingを有効にして,テストコードはRSpec.describe クラス名…から始めるようにしましょう.

4. 単一ファイルのテスト時に詳細な出力をする

spec_helper.rb
RSpec.configure do |config|
  config.default_formatter = "doc"
end

この設定をすると単一ファイルをテストする際,出力結果が詳細になります.
クラス名とメソッド名,各テストケースの説明が出力されます.

ClassName
  method_name
    behaves in one way under condition x
    behaves in another way under condition y

5. 実行時間が遅いテストを表示

spec_helper.rb
RSpec.configure do |config|
  config.profile_examples = 10
end

RSpec実行後,テスト結果と共に実行時間が遅いテストから上位10件表示されます.
ボトルネックになってる箇所の特定に使えそうです.
出力件数を変えることができますが,10件のままで良いと思います.

Top 10 slowest examples (0.35 seconds, 79.9% of total time):
  ClassName method_name behaves in one way under condition x
    0.12 seconds ./spec/models/class_name_spec.rb:12
  AnotherClassName another_method_name behaves in another way under condition y
    0.10 seconds ./spec/models/another_class_name_spec.rb:20
...

6. テストの実行順序を指定

spec_helper.rb
RSpec.configure do |config|
  config.order = :random
end

テストの実行順序をランダムにする設定です.この設定によって特定の順序に依存していないか検出することができます.また,ランダムな実行によって潜在的なエラーを発見することができます.

7.

spec_helper.rb
RSpec.configure do |config|
  Kernel.srand config.seed
end

乱数生成のシードを設定するための設定です.
これにより,常に同じ乱数のシーケンスを生成します.
ランダムな動作を一定に保ちつつ,テスト結果を再現することが可能です.

e.g.: シード値12345を用いて前回と同じ順序でテストを実行する場合

$ rspec --seed 12345

参考文献

追記(2024/05/08)

モンキーパッチの説明を追加しました.

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