アロー関数なし
アロー関数を使用する場合、ステップ間で状態を共有するには、ステップ外に状態を表す変数を作成する必要があることに注意してください!
let cukesState;
Given('I have {int} cukes in my belly', cukes => {
cukesState = cukes
})
ステップ定義は、式を持ち、それを1つ以上のGherkinステップに関連付けるメソッド関数ブロック関数関数です。CucumberがシナリオでGherkinステップを実行すると、実行する一致するステップ定義を探します。
これがどのように機能するかを示すために、次のGherkinシナリオを見てください
Scenario: Some cukes
Given I have 48 cukes in my belly
ステップのI have 48 cukes in my belly
の部分(Given
キーワードに続くテキスト)は、次のステップ定義と一致します
package com.example;
import io.cucumber.java.en.Given;
public class StepDefinitions {
@Given("I have {int} cukes in my belly")
public void i_have_n_cukes_in_my_belly(int cukes) {
System.out.format("Cukes: %n\n", cukes);
}
}
または、Java8ラムダを使用する
package com.example;
import io.cucumber.java8.En;
public class StepDefinitions implements En {
public StepDefinitions() {
Given("I have {int} cukes in my belly", (Integer cukes) -> {
System.out.format("Cukes: %n\n", cukes);
});
}
}
package com.example
import io.cucumber.java8.En
class StepDefinitions : En {
init {
Given("I have {int} cukes in my belly") { cukes: Int ->
println("Cukes: $cukes")
}
}
}
package com.example
import io.cucumber.scala.{ScalaDsl, EN}
class StepDefinitions extends ScalaDsl with EN {
Given("I have {int} cukes in my belly") { cukes: Int =>
println(s"Cukes: $cukes")
}
}
Given('I have {int} cukes in my belly') do |cukes|
puts "Cukes: #{cukes}"
end
const { Given } = require('cucumber')
Given('I have {int} cukes in my belly', function (cukes) {
console.log(`Cukes: ${cukes}`)
});
ステップ定義の式は、正規表現またはCucumber式のいずれかです。このセクションの例では、Cucumber式を使用しています。正規表現を使用する場合は、一致からの各キャプチャグループキャプチャグループキャプチャグループ出力パラメーター出力パラメーターは、ステップ定義のメソッド関数ブロック関数関数への引数として渡されます。
@Given("I have {int} cukes in my belly")
public void i_have_n_cukes_in_my_belly(int cukes) {
}
Given("I have {int} cukes in my belly") { cukes: Int ->
println("Cukes: $cukes")
}
Given("I have {int} cukes in my belly") { cukes: Int =>
println(s"Cukes: $cukes")
}
Given(/I have {int} cukes in my belly/) do |cukes|
end
Given(/I have {int} cukes in my belly/, function (cukes) {
});
キャプチャグループキャプチャグループキャプチャグループ出力パラメーター出力パラメーター式が、登録済みのパラメータータイプのregexp
のいずれかと同じである場合、キャプチャされた文字列は、ステップ定義のメソッド関数ブロック関数関数に渡される前に変換されます。上記の例では、組み込みのint
パラメータータイプのregexp
が\d+
であるため、cukes
引数は整数になります。
ステップ定義は、インスタンス変数に状態を格納することにより、状態を後続のステップ定義に転送できます。
ステップ定義は、特定のフィーチャーファイルまたはシナリオにリンクされていません。ステップ定義のファイル、クラス、またはパッケージ名は、どのGherkinステップに一致するかに影響しません。重要なのはステップ定義の式だけです。
Cucumberが一致するステップ定義のないGherkinステップを検出すると、一致するCucumber式を含むステップ定義スニペットを出力します。これを新しいステップ定義の出発点として使用できます。
次のGherkinステップを考えてみましょう
Given I have 3 red balls
一致するステップ定義がない場合、Cucumberは次のスニペットを提案します
@Given("I have {int} red balls")
public void i_have_red_balls(int int1) {
}
@Given("I have {int} red balls") { balls: Int ->
}
Given("I have {int} red balls") { balls: Int =>
}
Given('I have {int} red balls') do |int1|
end
Given("I have {int} red balls", function (int1) {
});
提案されたスニペットは、未定義のステップの一部と一致する場合、独自のパラメータータイプを使用します。colorパラメータータイプが存在する場合、Cucumberは提案された式でそれを使用します
@Given("I have {int} {color} balls")
public void i_have_color_balls(int int1, Color color) {
}
@Given("I have {int} {color} balls") { balls: Int, color: Color ->
}
Given("I have {int} {color} balls") { (balls: Int, color: Color) =>
}
Given('I have {int} {color} balls') do |int1, color|
end
Given("I have {int} {color} balls", function (int1, color) {
});
スニペットを印刷するには、Cucumberを実行するときにsummary
プラグインを使用していることを確認してください。
このドキュメントの改善にご協力ください。このページを編集。