Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

RSpec | rspec config | default_path

概要

rspec config | default_path

詳細

RSpecはデフォルトでspec配下のファイルをテスト対象とします。
これを変更する場合はdefault_pathオプションを利用します。

rspecコマンド呼び出し時に指定する
・.rspecファイルに設定する

などの方法があります。

サンプル仕様

単体テスト用のut_spec, 結合テスト用のit_specを別フォルダにしてプロジェクトのroot直下に作成。

単体は

rspec -fd --default_path ut_spec

結合は

rspec -fd --default_path it_spec

で呼び分ける。

構成

tree
┣ it_spec
| ┣ hige_spec.rb
| ┗ spec_helper.rb
┣ lib
| ┗ hige.rb
┗ ut_spec
    ┣ hige_spec.rb
    ┗ spec_helper.rb

ut_spec/hige_spec.rb

require './ut_spec/spec_helper'
require 'hige'

RSpec.configuration.debug = true if rand > 0.5

describe Hige do
  context "ut hige1", :hige1 => "hige1_outer" do
    it "ut hige1", :hige1 => "hige1_inner" do
      puts "case hige1" if RSpec.configuration.debug
      expect("hige1_inner").to eq(example.metadata[:hige1])
    end
  end

  it "ut hige2", :hige2 => "hige2" do
    puts "case hige2" if RSpec.configuration.debug
    expect("hige2").to eq(example.metadata[:hige2])
  end
end

it_spec/hige_spec.rb

require './it_spec/spec_helper'
require 'hige'

RSpec.configuration.debug = true if rand > 0.5

describe Hige do
  context "it hige1", :hige1 => "hige1_outer" do
    it "it hige1", :hige1 => "hige1_inner" do
      puts "case hige1" if RSpec.configuration.debug
      expect("hige1_inner").to eq(example.metadata[:hige1])
    end
  end

  it "it hige2", :hige2 => "hige2" do
    puts "case hige2" if RSpec.configuration.debug
    expect("hige2").to eq(example.metadata[:hige2])
  end
end

ut spec実行結果

$ rspec -fd --default_path ut_spec
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}

Hige
case hige2
  ut hige2
  ut hige1
case hige1
    ut hige1

Finished in 0.00201 seconds
2 examples, 0 failures

it spec実行結果

$ rspec -fd --default_path it_spec
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}

Hige
case hige2
  it hige2
  it hige1
case hige1
    it hige1

Finished in 0.001 seconds
2 examples, 0 failures