LoginSignup
1
1

More than 1 year has passed since last update.

Rspecで特定のテストだけ実行する

Posted at

特定のテストを実行

特定のテストを実行には2つあり、1つは実行したいテストのファイル名と行数を指定する方法。

ターミナル.
$ rspec spec/models/user_spec.rb:8

2つ目はテストコードitの前にfをつけるだけでそのテストのみ実行される方法。

task_spec.rb
require 'rails_helper'

RSpec.describe Task, type: :model do  
  describe 'validaton' do
    fit 'タスクを問題なく作成' do. #fitとなっているこのテストだけ実行される。
      task = build(:task)
      expect(task).to be_valid
      expect(task.errors).to be_empty
    end

    it 'タイトル空欄は無効' do
      task = build(:task,title: "")
      expect(task).to be_invalid
      expect(task.errors[:title]).to eq ["can't be blank"]
    end
  end
end

ターミナルで入力するよりも、fitと入力したほうが簡単なので、これが使えるように設定していく。

spec_helperを編集

spec_helper.rb
RSpec.configure do |config|
  config.filter_run_when_matching :focus
   #↑コメントアウトされているはずなので解除
end

これで、fdescribe, fcontext, fit のように使うことができる。

参考記事

RSpecで特定のテストを実行する方法

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