欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解Ruby on Rails中的Cucumber使用

程序员文章站 2024-01-07 13:49:40
    用 @wip (工作进行中)标签标记你未完成的场景。这些场景不纳入考虑,且不标记为测试失败。当完成一个未完成场景且功能测试通过时,...


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

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

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

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

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

    cucumber -p profile_name

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

    不要使用旧版的 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 的 读我文件获得更多说明。

上一篇:

下一篇: