• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

[追記] 次に書いた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/

   1    Scenario: A visitor successfully submits a new animal
   2      Given no animal named 'Alligator' exists
   3      When visitor goes to /animals/new
   4      Then visitor should see the Animal submission form
   5      When visitor submits an animal with name: 'Alligator', phylum: 'Chordata', animal class: 'Sauropsida', order: 'Crocodilia', family: 'Alligatoridae', genus: 'Alligator', and lay eggs:  'true'
   6      Then an animal named 'Alligator' should exist

Thenの後にWhenがくるのもありなのかも。

Stepの入れ子

http://wiki.github.com/aslakhellesoy/cucumber/calling-steps-from-step-definitions

   1  Given /^(.*) is logged in$/ do |name|
   2    Given "the user #{name} exists" 
   3    Given "I log in as #{name}" 
   4  end

記事作成のfeature

http://railscasts.com/episodes/155-beginning-with-cucumber

   1    Scenario: Create Valid Article
   2      Given I have no articles
   3      And I am on the list of articles
   4      When I follow "New Article"
   5      And I fill in "Title" with "Spuds"
   6      And I fill in "Content" with "Delicious potato wedges!"
   7      And I press "Create"
   8      Then I should see "New article created."
   9      And I should see "Spuds"
  10      And I should see "Delicious potato wedges!"
  11      And I should have 1 article
  12  
  13  #article_steps
  14  Given /^I have no articles$/ do
  15    Article.delete_all
  16  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 叙述の

   1   Story: Animal Submission
   2    As a Zoologist
   3    I want to add a new animal to the site
   4    So that I can share my animal knowledge with the community
   5  
   6    Scenario: successful submission
   7    Given I'm on the animal creation page
   8    When I add a new animal
   9    Then I should see the page for my newly created animal
  10    And the notice 'Thank you for your animal submission!'

   1  # animal_steps.rb
   2  When "I add a new animal" do
   3    fills_in 'Name', :with => 'Alligator'
   4    selects 'Chordata', :from => 'Phylum'
   5    fills_in 'Animal Class', :with => 'Sauropsida'
   6    fills_in 'Order', :with => 'Crocodilia'
   7    fills_in 'Family', :with => 'Alligatoridae'
   8    fills_in 'Genus', :with => 'Alligator'
   9    checks 'Lay Eggs'
  10    clicks_button 'Create'
  11  end

posted by Png satoko on Fri 17 Apr 2009 at 04:59

Comments:

or Preview
Social Bookmarks
  • Delicious
  • B_entry1440
  • Clip_16_12_w
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ