Ruby on Rails/Cucumberを試してみる http://www.ark-web.jp/sandbox/wiki/4294.html
cucumberとは †
- cucumberは、自然言語でテストを記述できる(?)テスティングフレームワーク、と理解してます。(まだ試してないので)
- 自然言語で書かれたフレーズを、正規表現で引っ掛けて、それへの具体的な処理を走らせることで、具体的な処理を走らせるようです。
この辺を参考にcucumberによる日本語の受け入れテスト記述とテスト自動化を体験してみるっす 
- Ruby on Rails - cucumber - GitHub
http://wiki.github.com/aslakhellesoy/cucumber/ruby-on-rails
- Cucumberがアツい - moroの日記
http://d.hatena.ne.jp/moro/20081112/1226486135
- Webratがスゴい(続:Cucumberがアツい) - moroの日記
http://d.hatena.ne.jp/moro/20081118/1226977015
環境用意 †
- 次のように環境用意
$ sudo gem install cucumber webrat $ rails cucumber_sample $ cd cucumber_sample $ ./script/generate cucumber
- これで、cucumberのファイル郡ができる
create features/step_definitions create features/step_definitions/webrat_steps.rb create features/support create features/support/env.rb create features/support/paths.rb exists lib/tasks create lib/tasks/cucumber.rake create script/cucumber
想定するアプリ †
- 今回は entry(titleとbodyをもつ) に対するCRUDをするアプリを考える
- なので、scaffoldで作ればOK
featureとstepの用意 †
- cucumberにはfeatureとstepという2つのファイルを記述する。
- featureが、受け入れテストのシナリオを書きく
- stepは、受け入れテスト中の、「やること」に対する、実際の実装を書く、という感じ。
- 今回は entry(titleとbodyをもつ) というリソースを考えて、これに対するテストを書くことにする
- entryに対するfeatureとstepファイルを作る
./script/generate feature entry title:string body:text
- これで、次の2つのファイルができる
exists features/step_definitions create features/manage_entries.feature create features/step_definitions/entry_steps.rb
実行してみる †
- 次のように実行
rake features
- 結果。
$ rake features (in /home/staff/shida/public_html/rails/cucumber_sample) Feature: Manage entries In order to [goal] [stakeholder] wants [behaviour] Scenario: Register new entry # features/manage_entries.feature:6 The DL driver for sqlite3-ruby is deprecated and will be removed in a future release. Please update your installation to use the Native driver. Given I am on the new entry page # features/step_definitions/webrat_steps.rb:6 undefined method `new_entry_path' for #<ActionController::Integration::Session:0xb71588c8> (NoMethodError) /home/staff/shida/public_html/rails/cucumber_sample/features/support/paths.rb:20:in `/^I am on (.+)$/' features/manage_entries.feature:7:in `Given I am on the new entry page' When I fill in "Title" with "title 1" # features/step_definitions/webrat_steps.rb:22 And I fill in "Body" with "body 1" # features/step_definitions/webrat_steps.rb:22 And I press "Create" # features/step_definitions/webrat_steps.rb:14 Then I should see "title 1" # features/step_definitions/webrat_steps.rb:93 And I should see "body 1" # features/step_definitions/webrat_steps.rb:93 Scenario: Delete entry # features/manage_entries.feature:14 Given the following entries: # features/step_definitions/entry_steps.rb:1 | title | body | | title 1 | body 1 | | title 2 | body 2 | | title 3 | body 3 | | title 4 | body 4 | uninitialized constant Entry (NameError) features/manage_entries.feature:15:in `Given the following entries:' When I delete the 3rd entry # features/step_definitions/entry_steps.rb:5 Then I should see the following entries: # features/step_definitions/entry_steps.rb:12 | title | body | | title 1 | body 1 | | title 2 | body 2 | | title 4 | body 4 | 2 scenarios 2 failed steps 7 skipped steps rake aborted! - scaffoldを用意してもう一回やってみる
./script/generate rspec_scaffold entry title:string body:text rake db:migrate rake features
- 結果がこんな感じ
Feature: Manage entries In order to [goal] [stakeholder] wants [behaviour] Scenario: Register new entry # features/manage_entries.feature:6 Given I am on the new entry page # features/step_definitions/webrat_steps.rb:6 When I fill in "Title" with "title 1" # features/step_definitions/webrat_steps.rb:22 And I fill in "Body" with "body 1" # features/step_definitions/webrat_steps.rb:22 And I press "Create" # features/step_definitions/webrat_steps.rb:14 Then I should see "title 1" # features/step_definitions/webrat_steps.rb:93 And I should see "body 1" # features/step_definitions/webrat_steps.rb:93 Scenario: Delete entry # features/manage_entries.feature:14 Given the following entries: # features/step_definitions/entry_steps.rb:1 | title | body | | title 1 | body 1 | | title 2 | body 2 | | title 3 | body 3 | | title 4 | body 4 | When I delete the 3rd entry # features/step_definitions/entry_steps.rb:5 Then I should see the following entries: # features/step_definitions/entry_steps.rb:12 | title | body | | title 1 | body 1 | | title 2 | body 2 | | title 4 | body 4 | 2 scenarios 9 passed steps
![[PukiWiki] [PukiWiki]](image/sandbox.gif)



