Tbpgr Blog

元エンジニア 人事 tbpgr(てぃーびー) のブログ

RSpec | rspec command | line_number option

概要

rspec command | line_number option

詳細

rspec commandのline_number optionで行数指定で実行します。

サンプルテストコード(hoge_spec.rb)

# encoding: utf-8
require "spec_helper"
require "hoge"

describe Hige do
  it "hoge1 1" do
    # some spec
  end

  it "hoge1 2" do
    # some spec
  end

  it "hoge2" do
    # some spec
  end
end

サンプルテストコード(hige_spec.rb)

# encoding: utf-8
require "spec_helper"
require "hige"

describe Hige do
  it "hige1 1" do
    # some spec
  end

  it "hige1 2" do
    # some spec
  end

  it "hige2" do
    # some spec
  end
end

rspec line_numberを試す

サンプルコードの行数表示
$ cat spec/hoge_spec.rb -n
     1  # encoding: utf-8
     2  require "spec_helper"
     3  require "hoge"
     4
     5  describe Hoge do
     6    it "hoge1 1" do
     7      # some spec
     8    end
     9
    10    it "hoge1 2" do
    11      # some spec
    12    end
    13
    14    it "hoge2" do
    15      # some spec
    16    end
    17  end
特定のdescribeを実行
$ rspec spec/hoge_spec.rb -l 5 -fs
Run options: include {:line_numbers=>[5]}

Hoge
  hoge1 1
  hoge1 2
  hoge2

Finished in 0.001 seconds
3 examples, 0 failures
特定のitを実行
$ rspec spec/hoge_spec.rb -l 6 -fs
Run options: include {:line_numbers=>[6]}

Hoge
  hoge1 1

Finished in 0 seconds
1 example, 0 failures
複数のitを実行
$ rspec spec/hoge_spec.rb -l 6 -l 10 -fs
Run options: include {:line_numbers=>[6, 10]}

Hoge
  hoge1 1
  hoge1 2

Finished in 0.001 seconds
2 examples, 0 failures