LoginSignup
11
10

More than 5 years have passed since last update.

RSpec3で特定のディレクトリのspecにメタデータを追加する

Posted at

前提

Sinatra+RSpec3+Capybara で簡単なテストを書いてみた

describe 'App' do
  before do
    visit '/'
  end
  it do
    expect(page).to have_content 'Hello, world'
  end
end

これでrspecを走らせたところ以下のようなエラーが

Failure/Error: visit '/'
NoMethodError:
  undefined method `visit' for #<RSpec::ExampleGroups::App::Nested1:0x007fc3f45531f0>
# ./spec/app_spec.rb:3:in `block (2 levels) in <top (required)>'

これ自体はmetadateの付け忘れでcapybaraを使う場合はdescribetype: :featureが必要だという話だった

describe 'App', type: :feature do
  # 中略
end

これはこれでいいんだけれど、ファイルが増えたら付け忘れが起きそうだしRailsの時はディレクトリに入れれば type 無しでいけた気がする…

RSpec::Core::Configuration#define_derived_metadata

みたいなことをツイートをしてたらリプライで教えていただいた

というわけで試してみた
今回は spec/features/ 以下のspecファイルにtype: :featureをつける

RSpec.configure do |config|
  config.define_derived_metadata(file_path: %r{/spec/features/}) do |metadata|
    metadata[:type] ||= :feature
  end
end

そして実行

$ rspec
.
Finished in 0.02655 seconds (files took 0.45621 seconds to load)
1 example, 0 failures

動いたーー

注意

このメソッド自体はRSpec3から入ったようなので、RSpec2では多分動きません

11
10
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
11
10