[追記] 次に書いたScenario, stepのリンク集もどうぞ:
http://blog.s21g.com/articles/1441
Given, When and Thenの基本はわかったんだけど、なんとなくスマートに書けるようになるにはまだ足りない気がする。というので色々snippetとして集めてみました。
(文法的にはもう古い物もあるので注意)
###Given, When and Then
基本はここで:
http://wiki.github.com/aslakhellesoy/cucumber/given-when-then
Given -> When -> Then の順にしかこないのかと思ったら、下記にシナリオの途中にあらわれるThenもあった:
http://www.benmabey.com/2008/02/04/rspec-plain-text-stories-webrat-chunky-bacon/
rails>>
Scenario: A visitor successfully submits a new animal
Given no animal named 'Alligator' exists
When visitor goes to /animals/new
Then visitor should see the Animal submission form
When visitor submits an animal with name: 'Alligator', phylum: 'Chordata', animal class: 'Sauropsida', order: 'Crocodilia', family: 'Alligatoridae', genus: 'Alligator', and lay eggs: 'true'
Then an animal named 'Alligator' should exist
<<--
Thenの後にWhenがくるのもありなのかも。
###Stepの入れ子
http://wiki.github.com/aslakhellesoy/cucumber/calling-steps-from-step-definitions
rails>>
Given /^(.*) is logged in$/ do |name|
Given "the user #{name} exists"
Given "I log in as #{name}"
end
<<--
###記事作成のfeature
http://railscasts.com/episodes/155-beginning-with-cucumber
rails>>
Scenario: Create Valid Article
Given I have no articles
And I am on the list of articles
When I follow "New Article"
And I fill in "Title" with "Spuds"
And I fill in "Content" with "Delicious potato wedges!"
And I press "Create"
Then I should see "New article created."
And I should see "Spuds"
And I should see "Delicious potato wedges!"
And I should have 1 article
#article_steps
Given /^I have no articles$/ do
Article.delete_all
end
<<--
###Scenarioは完結に、Stepで細かく
Imperative vs Declarative Scenarios
http://www.benmabey.com/2008/05/19/imperative-vs-declarative-scenarios-in-user-stories/
Scenarioを短くnarrativeに書いて、stepで細かいフォーム入力などを扱う感じに書かれていて一番しっくりきました。
Imperative 命令法/形の
Declarative => narrative 叙述の
rails>>
Story: Animal Submission
As a Zoologist
I want to add a new animal to the site
So that I can share my animal knowledge with the community
Scenario: successful submission
Given I'm on the animal creation page
When I add a new animal
Then I should see the page for my newly created animal
And the notice 'Thank you for your animal submission!'
<<--
rails>>
animal_steps.rb
When "I add a new animal" do
fills_in 'Name', :with => 'Alligator'
selects 'Chordata', :from => 'Phylum'
fills_in 'Animal Class', :with => 'Sauropsida'
fills_in 'Order', :with => 'Crocodilia'
fills_in 'Family', :with => 'Alligatoridae'
fills_in 'Genus', :with => 'Alligator'
checks 'Lay Eggs'
clicks_button 'Create'
end
<<--