LoginSignup
1
0

More than 5 years have passed since last update.

Turnipで書いたfeatureにパスに応じて異なるtypeをつける方法

Last updated at Posted at 2018-12-12

この記事はOkinawa.rb Advent Calendar 2018の12日目の記事です。
昨日は @hanachin_ さんの定数の名前を書き換えloadするimport_as gemの紹介でした。
明日は @hanachin_ さんのRubyでJavaScriptのTemplate literalsの真似をするです。

Turnipで複数のtypeのfeatureを書くサンプルアプリケーション

リポジトリは以下です
https://github.com/hanachin/turnip_example

以下の2つのTurnipのテストがあります。

  • spec/models/article.feature: metadata[:type]:modelのmodel spec
  • spec/system/article.feature: metadata[:type]:systemのsystem spec

Turnip.typeは常に1つ

Turnip.typeを設定するとTurnipのexampleのmetadataのtypeを変更することが出来ます。
でも、Turnip.typeは1つしか設定できない!

例えばmodel specもsystem specもGherkinで書きたくなったときに困ります

どうやってTurnipのexample groupに対して異なるtypeを設定するか

define_derived_metadataをつかうと上書きできます!べんり!

例えばspec/models/article.featureのtypeをmodelに変更するには以下のようにfile_pathオプションにfeatureにマッチするような正規表現を指定して、metadata[:type]を上書きします。

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

モデル以外のfeatureはsystem specとして動かしたい場合は以下のようにデフォルトのTurnip.type:systemに設定し、spec/models以下のfeatureのtypeを:modelに設定するといいです。

Turnip.type = :system
RSpec.configure do |config|
  config.define_derived_metadata(:file_path => %r{/spec/models/.*.feature}) do |metadata|
    metadata[:type] = :model
  end
end

まとめ

  • Turnip.typeを使うとTurnipのexample groupoのtypeを設定できる
  • Turnip.typeは1つしか設定できない
  • define_derived_metadataを使うとパスに応じてmetadataを書き換えられる

それでは、よいTurnipライフを〜 :hibiscus:

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