query: tag:cucumber
私の場合はCucumberのfeatureをもちろん英語で書いた方が楽なんだけど、日本で生活している以上、日本語で書いた方がいい場合もある。そのため、簡単な日英対訳表をCucumber v0.3.101時点の[languages.yml](http://github.com/aslakhellesoy/cucumber/blob/master/lib/cucumber/languages.yml)からまとめました。 フィーチャの定義 ===========
英語 (原文)日本語 (推奨)日本語 (alt)
Featureフィーチャ機能
Background背景-
Scenarioシナリオ-
Scenario Outlineシナリオアウトラインシナリオテンプレート
テンプレ
シナリオテンプレ
シナリオの定義 (Given-When-Then) ============
英語 (原文)日本語 (推奨)日本語 (alt)
Featureフィーチャ機能
Given前提-
Whenもし-
Thenならば-
Andかつ-
Butしかし但し
Examplesサンプル
参考リンク ====== * [Cucumberの翻訳定義ファイル](http://github.com/aslakhellesoy/cucumber/blob/master/lib/cucumber/languages.yml) * [moroさん作](http://d.hatena.ne.jp/moro/20090603/1244042258)の[misoというwebrat用 日本語step_definitionジェネレータ](http://github.com/moro/miso/tree)は便利だよ
posted by lchin lchin on Fri 18 Sep 2009 at 23:02 with 0 comments

前の記事のsnippets以外にも参考になった記事を集めておきます。

###Scenario, Features, Steps サンプル

  • シナリオやらいっぱい

http://itsignals.cascadia.com.au/?p=30

  • (ちょっと)冗長だけど参考になるシナリオ

http://github.com/aslakhellesoy/ba/blob/master/features/submit_proposal.feature

  • インスタンスのページを表示する(the post page)step
    /posts/1のページを表示するようなScenarioはどうかけばいいのん?と思っていたら、こんな風に書けるようです。

http://github.com/webjam/webjam/blob/d6b11f21ffabd224d26830c5b34839864d990c11/features/posts.feature
http://github.com/webjam/webjam/blob/d6b11f21ffabd224d26830c5b34839864d990c11/features/steps/posts_steps.rb

  • Open SourceでCucumber使っている例

http://wiki.github.com/aslakhellesoy/cucumber/examples

###Tutorials and Related Blog Posts

  • Tutorials and Related Blog Posts

http://wiki.github.com/aslakhellesoy/cucumber/tutorials-and-related-blog-posts

  • 20 articles on Cucumber and a free beverage recipe!

http://www.robbyonrails.com/articles/2009/04/09/20-articles-on-cucumber-and-a-free-beverage-recipe

###@variablesについての議論
http://www.ruby-forum.com/topic/181116

@userなどstep?scenario?にまたがる変数の使い方ですが、極力使わないのがスパゲッティ防止によさそうらしいです。900を超えるシナリオがある中で使ったけど、ポイント押さえて使えば大丈夫だったよというエピソードが出てきました。ただ、シーンに応じて使うのだったらありだろうというのが大方の見方かな。

###script/generate feature Article
body:stringなどとfieldも渡すと、それに応じてテーブルデータ等も生成してくれます:
http://d.hatena.ne.jp/amacou/20090127/1233058682

###Cookieの値が保持されないworkaround
http://stackoverflow.com/questions/681183/how-to-handle-cookies-when-testing-with-webrat

The workaround is use Webrat with Selenium back end. It runs all tests in a separate Firefox window, so cookies or javascript is not a problem. The downside is extra time and resources required to run Firefox and do all the real clicks, rendering etc.

他にもhackishなworkaroundが紹介されていました。

posted by satoko satoko on Fri 17 Apr 2009 at 05:11 with 0 comments

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

posted by satoko satoko on Fri 17 Apr 2009 at 05:01 with 0 comments

最新にしたらpaths.rbとBackgroundが追加されていたのでメモ。

###features/support/paths.rb
Cucumberを最新にしてfeatureをgenerateしようとしたらpaths.rbがないと怒られたので、script/generate cucumberでpaths.rbファイルを生成しました。

Scenarioで記述するページはここで定義:
rails>>
#features/support/paths.rb
module NavigationHelpers
def path_to(page_name)
case page_name
when /the homepage/
root_path
<<--

###Background
1feature内のScenarioに共通な前提条件を記述する。
Backgroundで書かれた条件は、各Scenario実行前、かつBefore Hookの後に実行されるようです。

The background is run before each of your scenarios but after any of your Before Hooks.
http://wiki.github.com/aslakhellesoy/cucumber/background

rails>>
Background:
Given a global administrator named “Greg”
And a blog named “Greg’s anti-tax rants”
And a customer named “Dr. Bill”
And a blog named “Expensive Therapy” owned by “Dr. Bill”
<<--

posted by satoko satoko on Fri 17 Apr 2009 at 04:04 with 0 comments

Cucumberのfeatureを実行すると、上記のエラーが出て困ったので探したら

I resolved it by uninstalling the old "bryanary-webrat" gem and making sure that "webrat" was up to date (0.4.3)

When you have both installed, ruby tends to load the old one instead.
http://groups.google.com/group/webrat/browse_thread/thread/d8ea685a1b1931a5#

古いwebrat gemを消しなさいと。

shell>>
gem search webrat

*** LOCAL GEMS ***

aslakhellesoy-webrat (0.3.2.2)
webrat (0.4.4, 0.4.3, 0.3.4)
<<--

案の定古いwebratがあったのでaslakhellesoy-webratをuninstallしたら動きました!

posted by satoko satoko on Tue 14 Apr 2009 at 15:15 with 0 comments

下記のページを参考にしました:
http://wiki.github.com/aslakhellesoy/cucumber/step-organisation

###モデル(テーブル)毎にstepsを作成する
原則はそれでいく、ただし認証などは別stepsとして作ったほうがベター:

  • employee_steps.rb
  • education_steps.rb
  • experience_steps.rb
  • authentication_steps.rb:認証用

all_steps.rbという巨大なファイルを作ることもできます。エレガントではないですけどね。

###モデルstepsには必ずGiven-When-Then stepを作る
モデルをカバーするようなstepsファイルでは、下記のCRUDをカバーするようなGiven-When-Then stepを書くことと。

  • 作成
  • 表示
  • 更新
  • 削除

###各stepsでは@variablesを作成して、オブジェクトの状態を保持できる
コントローラで取得する@userを下記のようなstepで記述できます:
rails>>
Give /^"(.*)"ユーザでログイン$/ do |login|
@user = User.find_by_login(login)
end
<<--
(注) 各stepで依存しあうことになり、再利用性が難しくなります
(注) 実際のログインシナリオはまた別にちゃんと記述する必要があります!

posted by satoko satoko on Fri 23 Jan 2009 at 15:42 with 0 comments

[追記 2009.01.20]script/generate featureの項を追加

moroさんの記事を読んで、Cucumber wktk!と思っていたのでtry

http://d.hatena.ne.jp/moro/20081112/1226486135
http://d.hatena.ne.jp/moro/20081118/1226977015
http://gist.github.com/26024

こちらの導入手順を参考しました:
http://wiki.github.com/aslakhellesoy/cucumber/ruby-on-rails

###必要なgemをinstall

  • rspec
  • rspec-rails
  • cucumber
  • webrat

ローカルには0.1.15を入れたのですが、念のため0.1.13で依存しているというgem:aslakhellesoy-webratもinstallしました。

shell>>
gem sources -a http://gems.github.com
sudo gem install aslakhellesoy-webrat
<<--

###Webratを使うようにCucumberのenvに書く
${RAILS_ROOT}/features/support/env.rb

shell>>
require "webrat"
Webrat.configure do |config|
config.mode = :rails
end
<<--

###script/generate feature
moroさんところのcucumberの構造辺りで紹介されているgeneratorでfeatureやstepsを生成:

shell>>
$ ruby script/generate feature Product
<<--

###Webratの仕様が変わってた
moroさんのgistを下記に保存して使用し始めたのですが、
shell>>
${RAILS_ROOT}/features/step_definitions/webrat_ja_steps.rb
<<--

細かい仕様が変更されていました:
ディレクトリstep_definitionsの位置も変わっているのに加えて、visits => visit、fills_in => fill_inなどが変更されていました。ただ、前もって調べなくても、rake featuresすると仕様変更された旨のmessageが出るので安心です。

rails>>
visit home_path
fill_in "Email", :with => "good@example.com"
<<--

またpendingされたstepには、下記のようにsnippetsが出力されて便利だなぁと思いました:

shell>>
You can use these snippets to implement pending steps which have no step definition:
When /^パラメータを入力する product_comment[body]=dummy comment!$/ do
end
<<--

###Refs
http://moriq.tdiary.net/20081022.html
http://d.hatena.ne.jp/hs9587/20081231/1230691812
http://barkingiguana.com/2008/11/11/getting-started-with-story-driven-development-for-rails-with-cucumber

posted by satoko satoko on Mon 19 Jan 2009 at 15:44 with 0 comments

札幌Ruby会議01で発表してきました
http://kakutani.com/20081025.html#p01

角谷さんの資料にあったcucumber。
サンプルがcucumberのgithubにあったのでリンク。

http://github.com/aslakhellesoy/cucumber/tree/master/examples
http://github.com/aslakhellesoy/cucumber/tree/master/examples/japanese_calculator
http://github.com/aslakhellesoy/cucumber/tree/master/examples/calculator

japanese_calculatorは角谷さん製です!

posted by satoko satoko on Mon 27 Oct 2008 at 13:25 with 0 comments