6. Cucumber – ビヘイビアの書き方
• シナリオを書く
Feature: FizzBuzz output
FizzBuzz is a game that has gained in popularity as a programming assignment to weed out non-programmers during job interviews. The
object of the assignment is less about solving it correctly according to the below rules and more about showing the programmer understands
basic, necessary tools such as if-/else-statements and loops. The rules of FizzBuzz are as follows:
For a given number X,
if X is divisible by 3 print Fizz;
if X is divisible by 5 print Buzz;
if X is divisible by 3 and 5 (15) print FizzBuzz;
else, print X.
Scenario: Fizz
Given I start FizzBuzz
When X is 3
Then I should see “Fizz”
7. Cucumber – ビヘイビアの書き方
• シナリオを書く
Feature: FizzBuzz
FizzBuzz is a game that has gained in popularity as a programming assignment to weed out non-programmers during job interviews. The
object of the assignment is less about solving it correctly according to the below rules and more about showing the programmer understands
basic, necessary tools such as if-/else-statements and loops. The rules of FizzBuzz are as follows:
For a given number X,
if X is divisible by 3 print Fizz;
if X is divisible by 5 print Buzz;
if X is divisible by 3 and 5 (15) print FizzBuzz;
else, print X.
ドキュメントを自然言語で書き
下す(コメント行)
Scenario: Fizz
Given I start FizzBuzz
When X is 3
Then I should see “Fizz”
ドキュメントが満たすべきシナ
リオ
→別で定義が与えられる
8. Cucumber – シナリオの各ステップ定義
• 1行1行定義を与える
• これを書く時点でFizzBuzzクラスを作っていなくても良い
• 最もその挙動に相応しい(と信じる)コードをとりあえず書く
Given I start FizzBuzz
When X is 3
Then I should see “Fizz”
Given /^I start FizzBuzz&/ do
@myfizzbuzz = FizzBuzz.new
End
When /^X is “*%d++”&/ do |number|
@myfizzbuzz.num = number
End
Then /^I should see “(*^”+)”&/ do |answer|
@myfizzbuzz.answer().should == answer
end
Given(動作前の状態)
When(いま行う動作)
Then(その動作に対応する挙動)
の形式で書くのが一般的
9. Rspecとの連携
• 抽象的な振る舞いを、より具体的な処理に落とし込んでテスト
を作る
• 処理を入れ子にした階層的な記述が可能
• 振る舞いだけでなく、実際の処理の流れを意識した記述になる
module FizzBuzz
describe “Fizz” do
it “3の倍数かつ5の倍数でないとき、Fizzが出力される” do
(↑の条件に合った数に対して、Fizzが出力されることを試すコード)
end
it “その他のとき、Fizz以外が出力される” do
(Fizz以外が出力されることを試すコード)
end
end
describe “Buzz” do
(後略)