详解Ruby on Rails中的Cucumber使用


    用 @wip (工作进行中)标签标记你未完成的场景。这些场景不纳入考虑,且不标记为测试失败。当完成一个未完成场景且功能测试通过时,为了把此场景加至测试套件里,应该移除 @wip 标签。
    配置你的缺省配置文件,排除掉标记为 @javascript 的场景。它们使用浏览器来测试,推荐停用它们来增加一般场景的执行速度。

    替标记著 @javascript 的场景配置另一个配置文件。

        配置文件可在 cucumber.yml 文件里配置。

    # 配置文件的定义:
    profile_name: --tags @tag_name

        带指令运行一个配置文件:

    cucumber -p profile_name

    若使用 fabrication 来替换假数据 (fixtures),使用预定义的 fabrication steps

    不要使用旧版的 web_steps.rb 步骤定义!最新版 Cucumber 已移除 web steps,使用它们导致冗赘的场景,而且它并没有正确地反映出应用的领域。

    当检查一元素的可视文字时,检查元素的文字而不是检查 id。这样可以查出 i18n 的问题。

    给同种类对象创建不同的功能特色:

  # 差
  Feature: Articles
  # ... 功能实作 ...

  # 好
  Feature: Article Editing
  # ... 功能实作 ...

  Feature: Article Publishing
  # ... 功能实作 ...

  Feature: Article Search
  # ... 功能实作 ...

    每一个功能有三个主要成分:
        Title
        Narrative - 简短说明这个特色关于什么。
        Acceptance criteria - 每个由独立步骤组成的一套场景。

    最常见的格式称为 Connextra 格式。

  In order to [benefit] ...
  A [stakeholder]...
  Wants to [feature] ...

这是最常见但不是要求的格式,叙述可以是依赖功能复杂度的任何文字。

    *地使用场景概述使你的场景备作它用 (keep your scenarios DRY)。

 

  Scenario Outline: User cannot register with invalid e-mail
   When I try to register with an email "<email>"
   Then I should see the error message "<error>"

  Examples:
   |email     |error         |
   |       |The e-mail is required|
   |invalid email |is not a valid e-mail |

    场景的步骤放在 step_definitions 目录下的 .rb 文件。步骤文件命名惯例为 [description]_steps.rb。步骤根据不同的标准放在不同的文件里。每一个功能可能有一个步骤文件 (home_page_steps.rb)
    。也可能给每个特定对象的功能,建一个步骤文件 (articles_steps.rb)。

    使用多行步骤参数来避免重复

    场景: 

User profile
   Given I am logged in as a user "John Doe" with an e-mail "user@test.com"
   When I go to my profile
   Then I should see the following information:
    |First name|John     |
    |Last name |Doe     |
    |E-mail  |user@test.com|

  # 步骤:
  Then /^I should see the following information:$/ do |table|
   table.raw.each do |field, value|
    find_field(field).value.should =~ /#{value}/
   end
  end

    使用复合步骤使场景备作它用 (Keep your scenarios DRY)

    # ...
    When I subscribe for news from the category "Technical News"
    # ...

    # 步骤:
    When /^I subscribe for news from the category "([^"]*)"$/ do |category|
      steps %Q{
        When I go to the news categories page
        And I select the category #{category}
        And I click the button "Subscribe for this category"
        And I confirm the subscription
      }
    end

    总是使用 Capybara 否定匹配来取代正面情况搭配 should_not,它们会在给定的超时时重试匹配,允许你测试 ajax 动作。 见 Capybara 的 读我文件获得更多说明。