Cucumberãã¢ãã
ä»äºã§ä½ã£ã¦ããRailsã¢ããªã«Cucumberãçªã£è¾¼ãã§ã¿ã¾ãããããã¯ç±ããããããååãã客ããã«è¦ãã¦åãã£ã¦ããããæ°ããã¾ãããã¶ãããã¡ããæºåã¯å¿ è¦ã ããåãå ¥ã仿§ããã¹ã¦ã客ããã«æ¸ãã¦ãããã¨ããã®ã¯é£ããã§ããã©*1ã
ã¨ããããå°å ¥ã¯ãã¡ããããæè¿ã¯Nokogiriãå¿ è¦ã§ãããã¨Terminal.appã§--no-colorã¤ããã«å®è¡ããã¨Terminal.appãã²ã©ããã¨ã«ãªãã®ã§iTermãè¦ãã§ãã
http://github.com/aslakhellesoy/cucumber/wikis/ruby-on-rails
2010-11-10
SEOçã«ããã®è¨äºãæ¸ãã¦ãã2å¹´ãããããªCucumberã®ä½¿ãæ¹ã調ã¹ã¾ããããã®ãã¦ãã¦ãé人åºçä¼ã«ã¦æ¬ã«ã¾ã¨ãã¾ããããããããã°ãã¡ããã©ããã
ä½¿ãæ¹
railsé¨éã®tophatenarãããã®ã§(ããã¾ãé¢ä¿ãªããã©)Railsåæã®è©±ãã§ãã
cucumberãã©ã°ã¤ã³ãã¤ã³ã¹ãã¼ã«ãã¦ãscript/generate cucumberããã¨ãRailsããã¹ãããããã®ããããªãã¡ã¤ã«ãçæããã¾ãããã¨ãã°script/cucumberã«å®è¡ã¹ã¯ãªãããå ¥ãã¾ãã
å·®ããããéè¦ãªã®ã¯ä»¥ä¸ã®äºã¤ã»ã©ã
- features/support/env.rb
- cucumberå ¨ä½ã®è¨å®ãåãã¡ã¤ã«ã§ããæåã¯ç·¨éããªãã¦ããã§ãããã
- features/step_definitions/webrat_steps.rb
- webratã¨ãããWebã¢ããªã®ãã¹ãç¨ãã¼ã«ã使ãããã®ã³ã¼ãã§ãããããç·¨éããªãã¦ããã¯ãã
cucumberã®æ§é
cucumberã§ã¯ãã¾ãfeatures/hoge.featureã¨ããå½åè¦åã§featureãã¡ã¤ã«ãæ¸ãã¾ããããããèªç¶è¨èª(ã½ã)ãããã¬ã¼ã³ããã¹ããã§ãåãå ¥ã仿§ãè¨è¿°ããããã¡ã¤ã«ã¨ãªãã¾ãã
ãã¨ãã°æ¬¡ã®ãããªã³ãã³ãã§åãå ¥ã仿§ã®ã²ãªå½¢ãä½ãã¾ãã
$ ruby script/generate feature Entry exists features/step_definitions create features/manage_entries.feature create features/step_definitions/entry_steps.rb
ããã§ã§ããfeatures/manage_entries.featureãfeatureãã¡ã¤ã«ã§ãããä¸èº«ã¯ãããªã®ã
$ cat features/manage_entries.feature Feature: Manage entries In order to keep track of entries A entry mechanic Should be able to manage several entries Scenario: Register new entry Given I am on the new entry page And I press "Create" Scenario: Delete entry Given there are 4 entries When I delete the first entry Then there should be 3 entries left More Examples: | initial | after | | 100 | 99 | | 1 | 0 |
ã©ãã¿ã¦ããã¬ã¼ã³ããã¹ãã§ããããã°ãããããããå®è¡ã§ããã®ãCucumberã§ãã
ã§ãã©ãã¿ã¦ããã¬ã¼ã³ããã¹ããªã®ã§ããã®ã¾ã¾ã§ã¯å®è¡ã§ãã¾ãããããããªãã¨ããã¦å®è¡ããããã®ä»çµã¿ãstepsã¨å¼ã°ãã¦ãããã®ã§ããå ·ä½çã«ã¯ features/step_definitions/entry_steps.rb ã§ããã
$ cat features/step_definitions/entry_steps.rb Given /I am on the new entry page/ do visits "/entries/new" end Given /there are (\d+) entries/ do |n| Entry.transaction do Entry.destroy_all n.to_i.times do |n| Entry.create! :name => "Entry #{n}" end end end When /I delete the first entry/ do visits entries_url clicks_link "Destroy" end Then /there should be (?d+) entries left/ do |n| Entry.count.should == n.to_i response.should have_tag("table tr", n.to_i + 1) # There is a header row too end
ã¨ãããã¨ã§featureãã¡ã¤ã«ã®æè¨ã«ããããããããªæ£è¦è¡¨ç¾ã¨ãå®éã®Rubyã³ã¼ãã並ãã§ãã¾ããå ·ä½çã«ã¯ã¤ã³ãã³ãã¬ãã«2ãããã®GivenãAndãWhenãThenã®æè¨ã«å¯¾å¿ãã¦ãã¾ãã
Cucumberã¯å®è¡æã«featureãã¡ã¤ã«ãè§£æããæè¨ããããããstepã®å¦çãå®è¡ãã¦ãã¹ããã¾ããstepã¯æ£è¦è¡¨ç¾ã使ãã¾ãã®ã§ãæç« ä¸ã®ä¸é¨ãåçã«åãåºããã¨ãåºæ¥ã¾ãããã¨ãã° "Given there are 4 entries"ã¨ããfeatureå ã®æè¨ã«å¯¾ããGiven /there are (\d+) entries/ ã¨ããstepãããããã¦ã"4"ãåçã«æå®ã§ãã¦ããã¤ã¡ã¼ã¸ãä¼ããã¯ãã
ã§ãGivenãWhenãThenã¯ãããããåæãå¦çå 容ããã®çµæ(ã«å¯¾ããæ¤è¨¼)ãã¨ãªãã¾ãããããä¸ããé ã«æ¸ãã¦ããããã§ãããAndã¯åã®stepã¨åãäºãç¹°ãè¿ãã¾ããThenã§ã¯æ¤è¨¼ããããã§ãããæ¤è¨¼ã«ã¯RSpecã®èªå½(should/should_not)ã使ãã¾ãã
ã¾ãããã®ã·ããªãªãæ¥æ¬èªã§æ¸ãã¦ã¿ãã°ãããªæãã§ãã
Scenario: æ°ããã¨ã³ããªã®ç»é² åæ I am on the new entry page ã㤠I press "Create" Scenario: ã¨ã³ããªã®åé¤ åæ there are 4 entries ãã I delete the first entry ãªãã° there should be 3 entries left
ããã«ã"there are 4 entries"ã¨ãã®ã¨ããã¯stepã§æ£è¦è¡¨ç¾ã§æå®ãã¦ã¾ãããã®ããstepã«æ¬¡ã®ãããªå®ç¾©ãããã°
Given /^(\d+).? ã®ã¨ã³ããªããã/ do |n| Entry.transaction do Entry.destroy_all n.to_i.times do |n| Entry.create! :name => "Entry #{n}" end end
ããæ¸ãã¾ãã
Scenario: ã¨ã³ããªã®åé¤ åæ 4㤠ã®ã¨ã³ããªããã ãã I delete the first entry ãªãã° there should be 3 entries left
ãããªæãã§stepãå å®ããã¦ããã°OKã
ã¨ãããã§çµããã¨é¢åãªã ããããã¨æãããããã¾ããããæå¤ã¨ãã¹ãã«æ¸ãèªå½(=ãããã¨)ã¯æ±ºã¾ã£ã¦ããããªãã§ãããããExcelã§ãã¹ã仿§æ¸æ¸ãã¦ã¦ãããªã®ã§ãä¸åã¤ãã£ã¦ãã¾ãã°æå¤ã¨ä½¿ãåããããªæ°ããã¦ã¾ãã
ã§ãå®ã¯ããã§æ¸ãããåæãã¨ãã¯è§è°·ããã®å°½åã«ãããã®ã¾ã¾ä½¿ãã¾ãã
http://github.com/aslakhellesoy/cucumber/commit/9291e7c8d1de8189dfda3d2d7318277731fb9c5b
ã¨ãããã¨ã§ããå°ã詳ããä½¿ãæ¹ã¯ãã¨ã§æ¸ãã
Railsã¨ã®çµ¡ã¿
ç®æ¡æ¸ãã§ã
- Railsã®ãã¹ããããããã«webratã使ã£ã¦ã
- webratã§HTMLãæ¤è¨¼ããããã«Nokogiri使ã£ã¦ã
- Railsã®ãã¹ãã¯IntegrationTestã®ã¬ã¤ã¤ã§ãActionController::Integration::Sessionã使ã£ã¦ã¾ãã
- ããã¯ActionController::Dispatcher.dispach ããå ãæ¤è¨¼ãã¦ãã
- ãããwebratã®Sessionã§ãããã§ä½¿ãæãã
- IntegrationTestã®å¶ç´ã¯å½ç¶åãã
- fixturesã使ããã¯ã
OpenIDã®ä»¶ã¯ãã¨ã§æ¸ããã¨ãããã¨ã§ç§ã®Twitterãã¿ã¦wktkãã¦ã人ã«ã¯ã²ã©ããã¨ããã¾ãããããããããªããããã¨ã§æ¸ãã¾ãã
*1:ãããã*ãã¹ã¦*ã客ããã«æ¸ãã¦ãããã®ãããã¾ãç®æãã¦ããªããã¡ããã¨ã·ã¹ãã ãã¿ãªããä¼è©±ããèªå½ã¨ãã¦ã¯ä½¿ããã