9. Gherkinキーワード
• Feature • Given
• Background • When
• Scenario • Then
• Scenario Outline • And
• Scenarios • But
• Examples •| |、” ”、#
10. $ cucumber --i18n ja
| feature | "フィーチャ", "機能" |
| background | "背景" |
| scenario | "シナリオ" |
| scenario_outline | "シナリオアウトライン", "シナリオテンプレート", "テンプレ", "シナリオテンプレ" |
| examples | "例", "サンプル" |
| given | "* ", "前提" |
| when | "* ", "もし" |
| then | "* ", "ならば" |
| and | "* ", "かつ" |
| but | "* ", "しかし", "但し", "ただし" |
| given (code) | "前提" |
| when (code) | "もし" |
| then (code) | "ならば" |
| and (code) | "かつ" |
| but (code) | "しかし", "但し", "ただし" |
11. シナリオの書き方
Feature: Traveler books room
ln order to reduce staff
As a hotel owner
l want travelers to book rooms on the web
Scenario: Successful booking
Scenario: Hotel is full
Scenario: visitor forgets to enter email
12. シナリオの書き方
# language:en
Feature: Traveler books room
ln order to reduce staff
As a hotel owner
l want travelers to book rooms on the web
Scenario: Successful booking
Given a hotel with “5” rooms and “0” bookings
13. $ cucumber
# language:en
Feature: Traveler books room
ln order to reduce staff
As a hotel owner
l want travelers to book rooms on the web
Scenario: Successful booking # features/book_room.feature:7
Given a hotel with “5” rooms and “0” bookings # features/book_room.feature:8
1 scenario (1 undifined)
1 step (1 undefined)
0m0.001s
You can implement step definitions for undefined steps with these snippets:
Given /^a hotel with “([^”])” rooms and “([^”])” bookings$/ do |arg1, arg2|
pending # express the regexp above with the code you wish you had
end
19. 命令型のシナリオ
Scenario: transfer money (imperative)
Given I have $100 in Checking
And I have $20 in savings
When I go to the transfer form
And I select "Checking" from "Source Account"
And I select "Savings" from "Target Account"
And I fill in "Amount" with "15"
And I press "Execute Transfer"
Then I should see that l have $85 in checking
And I should see that l have $35 in savings
20. 宣言型のシナリオ
Scenario: transfer money (declarative)
Given I have $100 in checking
And I have $20 in savings
When I transfer $15 from checking to savings
Then I should have $85 in checking
And I should have $35 in savings
21. フィーチャの構成
features $ cucumber features
insurance $ cucumber features/insurance
medical $ cucumber features/insurance/medical
dental $ cucumber features/insurance/ dental
life $ cucumber features/insurance/ life
disability $ cucumber features/insurance/ disability
pto $ cucumber features/pto
accrual $ cucumber features/pto/accrual
usage $ cucumber features/pto/usage
29. WorldをObject以外にする
class MyWorld
def some_helper
・・・
end
end
World do
MyWorld.new
end
• cucumber-railsは、WorldをActionController::IntegrationTest
のインスタンスとして設定している
30. ステップ定義から
ステップ定義を呼び出す
When I select checking as the source account
And I select savings as the target account
And I set $20.00 as the amount
And I click transfer
When I transfer $20.00 from checking to savings
31. When /I transfer (.*) from (.*) to (,*)/ do |amount, source, target|
When "I select #{source} as the source account"
When "I select #{target} as the target account"
When "I set #{amount} as the amount"
When "I click transfer"
end
When /I transfer (.*) from (.*) to (.*)/ do |amount, source, target|
steps %Q{
When I select #{source} as the source account
And I select #{target} as the target account
And I set #{amount} as the amount
And I click transfer
}
end
34. タグ付きフック
Before("@foo") do
puts "This will run before each scenario tagged with @foo"
end
Before("@foo,~@bar", "@zap") do
puts "This will run before each scenario tagged with @foo or not @bar AND @zap"
end
36. Feature: Invite friends
Background: Logged in
Given I am logged in as "Aslak"
And the following people exist:
| name | friend? |
| David | yes |
| Vidkun | no |
Scenario: Invite someone who is alreadv a friend
Scenario: Invite someone who is not a friend
Scenario: Invite someone who doesn't have an account
37. マルチラインテキスト
Scenario: pending implementation
Given a file named "example_without_block_spec.rb" with:
"""
describe "an example" do
it "is a pending example" end
"""
When I run "rspec example_without_block_spec.rb"
Then the exit code should be 0
And the output should contain "1 example, 0 failures, 1 pending"
And the output should contain "Not Yet Implemented"
And the output should contain "example_without_block_spec.rb:2"
41. Scenario Outline:
submit guess Given the secret code is "<code>"
When I guess "<guess>"
Then the mark should be "<mark>"
Scenarios: all numbers correct
| code | guess | mark |
| 1234 | 1234 | ++++ |
| 1234 | 1243 | ++-- |
| 1234 | 1423 | +--- |
| 1234 | 4321 | ---- |
42. Scenario Outline:
Given a discount of <discount> When I order the
following book:
| title
| price
|
| Healthy eating for programmers | <price> |
Then the statement should read:
"""
Statement for David
Total due:
<total>
"""
Scenarios:
| discount | price | total |
| 10%
| $29.99 | $26.99 |
| 15%
| $29.99 | $25.49 |