详解Ruby on Rails中的Cucumber使用
用@wip(工作进行中)标签标记你未完成的场景。这些场景不纳入考虑,且不标记为测试失败。当完成一个未完成场景且功能测试通过时,为了把此场景加至测试套件里,应该移除@wip标签。
配置你的缺省配置文件,排除掉标记为@javascript的场景。它们使用浏览器来测试,推荐停用它们来增加一般场景的执行速度。
替标记著@javascript的场景配置另一个配置文件。
配置文件可在cucumber.yml文件里配置。
#配置文件的定义: profile_name:--tags@tag_name
带指令运行一个配置文件:
cucumber-pprofile_name
若使用fabrication来替换假数据(fixtures),使用预定义的fabricationsteps。
不要使用旧版的web_steps.rb步骤定义!最新版Cucumber已移除websteps,使用它们导致冗赘的场景,而且它并没有正确地反映出应用的领域。
当检查一元素的可视文字时,检查元素的文字而不是检查id。这样可以查出i18n的问题。
给同种类对象创建不同的功能特色:
#差 Feature:Articles #...功能实作... #好 Feature:ArticleEditing #...功能实作... Feature:ArticlePublishing #...功能实作... Feature:ArticleSearch #...功能实作...
每一个功能有三个主要成分:
Title
Narrative-简短说明这个特色关于什么。
Acceptancecriteria-每个由独立步骤组成的一套场景。
最常见的格式称为Connextra格式。
Inorderto[benefit]... A[stakeholder]... Wantsto[feature]...
这是最常见但不是要求的格式,叙述可以是依赖功能复杂度的任何文字。
自由地使用场景概述使你的场景备作它用(keepyourscenariosDRY)。
ScenarioOutline:Usercannotregisterwithinvalide-mail WhenItrytoregisterwithanemail"<email>" ThenIshouldseetheerrormessage"<error>" Examples: |email|error| ||Thee-mailisrequired| |invalidemail|isnotavalide-mail|
场景的步骤放在step_definitions目录下的.rb文件。步骤文件命名惯例为[description]_steps.rb。步骤根据不同的标准放在不同的文件里。每一个功能可能有一个步骤文件(home_page_steps.rb)
。也可能给每个特定对象的功能,建一个步骤文件(articles_steps.rb)。
使用多行步骤参数来避免重复
场景:
Userprofile GivenIamloggedinasauser"JohnDoe"withane-mail"user@test.com" WhenIgotomyprofile ThenIshouldseethefollowinginformation: |Firstname|John| |Lastname|Doe| |E-mail|user@test.com| #步骤: Then/^Ishouldseethefollowinginformation:$/do|table| table.raw.eachdo|field,value| find_field(field).value.should=~/#{value}/ end end
使用复合步骤使场景备作它用(KeepyourscenariosDRY)
#...
WhenIsubscribefornewsfromthecategory"TechnicalNews"
#...
#步骤:
When/^Isubscribefornewsfromthecategory"([^"]*)"$/do|category|
steps%Q{
WhenIgotothenewscategoriespage
AndIselectthecategory#{category}
AndIclickthebutton"Subscribeforthiscategory"
AndIconfirmthesubscription
}
end
总是使用Capybara否定匹配来取代正面情况搭配should_not,它们会在给定的超时时重试匹配,允许你测试ajax动作。见Capybara的读我文件获得更多说明。