rspecæ¬ä¸äººèªæ¸ä¼ã®ç¶ããcucumberã®ãåå¼·ã¡ã¢ã§ãã
19.1 Step Definitions
å®ç¾©ããã¦ããªãStepãå«ãfeatureãå®è¡ããã¨ãcucumberã¯Step Definifionã®ã²ãªå½¢ãåºåãã¦ãããã
19.2 World
å ¨ã¦ã®ã·ããªãªã¯Worldãªãã¸ã§ã¯ãã®ã¤ã³ã¹ã¿ã³ã¹ã®æèã§å®è¡ããããWorldãªãã¸ã§ã¯ãã¯ããã©ã«ãã§ã¯åãªãObjectã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ã
Worldã使ã£ã¦ãåstep_definitionã§ä½¿ããã«ãã¼ã¡ã½ãããå®ç¾©ããã«ã¯ä¸è¨ã®ããWorldã¡ã½ããã®å¼æ°ã«Moduleãæå®ãã¦ãããã
module MyHelper def some_helper # ... end end World(MyHelper)
ã©ãã§ä¸è¨ã®ãããªè¨å®ããã¦ããããã©ã features/support/world.rb ã«æ¸ãã®ããããããããã
Worldã®å ã¨ãªãã¯ã©ã¹ãå¥ã®ãã®ã«ãããã¨ãåºæ¥ããä¸è¨ã®ããã«Worldã¡ã½ããã®ãããã¯ã«ã¤ã³ã¹ã¿ã³ã¹ãå ¥ãã¦ããã¨åºæ¥ãã¿ããã
class MyWorld def some_helper #... end end World do MyWorld.new end
ä¸è¨ã¯cucumber-railsã®ããã«cucumberã®ãã©ã°ã¤ã³ã使ãæã®ãã¯ããã¯ã
19.3 Calling steps within step definitions
stepãDRYã«ããæ¹æ³ã®ãã¡ã®ä¸ã¤ã¯ãé«ãã¬ãã«ã®æ½è±¡åãããstepãä½ããã¨ãããããã¨ãã¨ãã°ä¸è¨ã®ãããªæ¸ãæ¹ã«ãªãã¯ãã
When /I transfer (.*) from (.*) to (.*)/ do |amount, source, target| When "I select #{source} as the source account" When "I select #{target} as the target account" When "I set #{amount} as the amount" When "I click transfer" end
ã§ãä¸è¨ã®ããã«ãæ¸ããã
When /I transfer (.*) from (.*) to (.*)/ do |amount, source, target| steps %Q{ When I select #{source} as the source account And I select #{target} as the target account And I set #{amount} as the amount And I click transfer } end
ã©ã¡ãã§ãåããªã®ã§æ¸ããããæ¹ãé¸æããã¨ããã
ä¸è¨ã®äºã¤ã®ä¾ã¯ãstep definitionsããstepãå¼ãã§ãããããã¯DRYçã«ã¯ãããã©stepãstepå¼ãã§ããããå¥ã®stepå¼ãã§ã»ã»ã»ã¨ãªãã¨ãããã°ãã«ãããªããã¾ããæ½è±¡æ§ã«ã°ãã¤ãã®ããstep definitionsã«ãªãããããã©ããããã¯ãã©ã³ã¹ãã¿ã¦æ±ºããã»ããããã
19.4 Hooks
cucumberã¯ä¸è¨ã®ä¸ã¤ã®ã¡ã½ããã§ããã¯ãå®ç¾ãã¦ããã
- Before
- scnenarioã®å
- After
- scenarioã®å¾
- AfterStep
- stepã®å¾
hookã¯featuresé ä¸ãªãã©ãã§ãå®ç¾©ã§ãããã©features/support/hooks.rbã«æ¸ãã®ããããããããã
ã¾ããhookã¯ä½åã§ãå®ç¾©ã§ãããä¾ãã°Beforeã10åãã£ã¦ãåé¡ãªãã
tagged hooks
ç¹å®ã®scenarioã ãã§å®è¡ããããhookãããã¨ãã«ä½¿ãã
Before("@foo") do puts "This will run before each scenario tagged with @foo" end
ã³ãã³ãã©ã¤ã³ã§ã®--tagsã®ããã«ããã¨ãã£ã¨è¤éãªæ¡ä»¶ã使ãã
Before("@foo,~@bar", "@zap") do puts "This will run before each scenario tagged with @foo or not @bar AND @zap" end
visibility
hookã¯ä¾¿å©ã ãã©æè¡è 以å¤ã«ã¯ããåãããªãã¨ããæ¬ ç¹ããããæè¡è 以å¤ã®äººã«ãããããã«ãããå ´åã¯Backgroundã使ãã
19.5 Background
Backgroundã¯ä¸ããããfeatureå ã®å ¨ã¦ã®ã·ããªãªã®åã«å®è¡ããããfeatureã®ãªãã§åç §ãããã¨ãã«Before hookã®ä»£ããã¨ãã¦ä½¿ããFeatureå ã§å ±éããGivenãããã£ã¦Backgroundã«ã ãã¦ããã¨è¦éããè¯ããªãããã
Beforeã¯Backgroundããåã«å®è¡ãããã
19.6 Multi-line Text
ä¸è¨ã®ããã«ããã¨è¤æ°è¡ã®ããã¹ããå¼æ°ã«ã¨ãããmarginã¯æåã®"ã§æ±ºã¾ãã
Scenario: pending implementation Given a file named "example_without_block_spec.rb" with: """ describe "an example" do it "has not yet been implemented" end """ When I run "spec example_without_block_spec.rb" Then the exit code should be 0 And the stdout should include """ Pending: an example has not yet been implemented \(Not Yet Implemented\) .\/example_without_block_spec.rb:2 Finished in ([\d\.]*) seconds 1 example, 0 failures, 1 pending """ # ...
è¤æ°è¡ã®ããã¹ãã¯æ£è¦è¡¨ç¾ã§ãã£ããã£ããå¿ è¦ã¯ãªããæ£è¦è¡¨ç¾ã«å«ã¾ããªãã£ãæååã¯ãããã¯ã®æå¾ã®å¼æ°ã«ã¯ããã
Given /a file named "([^\"]*)" with:/ do |filename, text| # ... end Then /the stdout should include/ do |text| # ... end
stepä¸ã®æåã¯æ£è¦è¡¨ç¾ã§è©ä¾¡ãããã®ã§ãè¤æ°è¡ã®ããã¹ãä¸ã«()çã®ãæ£è¦è¡¨ç¾ã§ä½¿ãããç¹å¥ãªæåãããå ´åã¯ãããå ´åã¯escapeããã¦ããå¿ è¦ããã
19.7 Tables in Steps
wikiã¹ã¿ã¤ã«ã®ãã©ã¼ãããã§tableã表ç¾ã§ãããcucumberã¯è¡ã®æåã« "|" ãè¦ã¤ããã¨ãã«ãã¼ã¹ãã¦ãCucumber::Ast::Tableãªãã¸ã§ã¯ãã«ãããCucumber::Ast::Tableãªãã¸ã§ã¯ãã¯ãhashã®é åãè¿ãhashesã¡ã½ãããæã£ã¦ããã
Cucumberã¯Cucumber::Ast::TableãStep definitionä¸ã®æå¾ã®ãããã¯å¼æ°ã¨ãã¦æ¸¡ãã
Scenario Outlines
Scenariosãã¼ã¯ã¼ãã§ãScenario Outlineã«æµãè¾¼ããã¼ã¿ããã¼ãã«ã§å®ç¾©ã§ããã
Scenario Outline: submit guess Given the secret code is "<code>" When I guess "<guess>" Then the mark should be "<mark>" Scenarios: all numbers correct | code | guess | mark | | 1234 | 1234 | ++++ | | 1234 | 1243 | ++-- | | 1234 | 1423 | +--- | | 1234 | 4321 | ---- |
Scenariosã®ã¨ã¤ãªã¢ã¹ã¨ãã¦Examplesãã¼ã¯ã¼ããããã
è¤æ°è¡ã®ããã¹ããããã¼ãã«ã®å¤ã«ãscenario outlineã¯ä½¿ããã
Scenario Outline: Given a discount of <discount> When I order the following book: | title | price | | Healthy eating for programmers | <price> | Then the statement should read: """ Statement for David Total due: <total> """ Scenarios: | discount | price | total | | 10% | $29.99 | $26.99 | | 15% | $29.99 | $25.49 |
19.9 Configuration
cucumberã®ãªãã·ã§ã³ããããããæ¸ãã¦ããããã®ãã¡ã¤ã«ã¨ã㦠cucumber.yml ã¾ã㯠config/cucumber.yml ãå®ç¾©ããã¦ããã
ä¾ãã° cucumber.yml ã«ä¸è¨ã®ããã«æ¸ãã¦
wip: --tags @wip features
ä¸è¨ã®ããã«å®è¡ããã¨
cucumber -p wip
ä¸è¨ã®ããã«ããã®ã¨åãçµæã«ãªã
cucumber --tags @wip features
rails ã ã¨ãèªåçã«defaultã¨è¨ãååã®profileãæ¢ããããããªã®ã§
default: --format pretty
ãªã©ã¨ãã¦ããã¨å¤§å¤é½åãããã