LoginSignup
3
1

More than 5 years have passed since last update.

私もTurnipのセットアップにはまったので、うまく行った方法をまとめました。

Last updated at Posted at 2015-12-12

結構はまって、英語のドキュメントとか色々読んだのでまとめました。

Gemfileにturnipを入れる

group :test do
 gem 'turnip'
end

記述したら、

bundle install

turnip_helper.rbを作る

turnip_helperとは、spec_helperみたいなものです。turnipがturnip_helperを自動で読み込んでくれるみたいです。

$ touch spec/turnip_helper.rb

ファイルを作成後turnip_helper.rbに以下のコードを書きます。

require 'rails_helper'
# 後に作る_steps.rbというテストファイルを読みこむためのコード
Dir.glob("spec/steps/**/*steps.rb") { |f| load f, true }

.rspecにturnipのためのオプションを追加

.rspecとは、rspecを実行するときにデフォルトでつけるオプションを定義して置ける場所です。
.rspecが無くても、手動でオプションをつけて実行すれば同じ結果が得られます。

.rspec

-r turnip/rspec

を追加。

※.rspecファイルがトップディレクトリになければ、 rails g rspec:install で作って下さい。
これで環境構築は終わったはずです。次から実際にテストを書いていきましょう。

featuresディレクトリに、.featureファイルを作成

.featureファイルとは、テストシナリオを自然言語で記述するためのDSLです。
gherkinというライブラリを拡張したものらしいです。

$ touch spec/features/battle.feature

battle.feature

Feature: 戦闘
 Scenario: 柔い敵と戦闘
 Given "スライム" が現れた!
 When 勇者の攻撃!
 Then モンスターを倒した

 Scenario: 固い敵と戦闘
 Given "はぐれメタル" が現れた!
 When 勇者の攻撃!
 Then モンスターを倒せなかった

分かりやすいので、この例を使いました。

bundle exec rspec

スクリーンショット 2015-12-12 18.16.34.png

現段階だとこんな感じでpendingのテストケースが出てくるはずです。
No such step と書かれているので、次に実際にシナリオに対応したstepを記述します。

stepsディレクトリに、_step.rbファイル作成

$ touch spec/steps/battle_step.rb

battle_step.rb

# coding: utf-8

MONSTERS = [
 ['スライム', 2],
 ['はぐれメタル', 500]
]

step ":monster が現れた!" do |name|
 @defence = MONSTERS.assoc(name).last
end

step "勇者の攻撃!" do
 @defence -= 3
end

step "モンスターを倒した" do
 expect(@defence).to be <= 0
end

step "モンスターを倒せなかった" do
 expect(@defence).to be > 0
end

できました。実は :monsterが、featureの"スライム"、"はぐれメタル"に対応しています。

bundle exec rspec

Kobito.5SOgYV.png

こんな感じででてきたら完了です。
次からは、turnipの文法と、capyaraを組み合わせて受け入れテストの自動化を進めていきましょう。

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