cucumber

更新时间:2022-11-25 03:19:34 阅读: 评论:0


2022年11月25日发(作者:黑神锅传奇)

⼀个简单的Cucumber+Capybara的BDD开发例⼦

Cucumber是⼀个⽤来做BDD(BehaviorDrivenDesign)的⼯具。你可以⽤⾃⼰的语⾔来写场景(scenarios)和定义(definitions).

Capybara可以⽤来模拟⽤户对浏览器(browr)的访问。

下载和配置

下载cucumber到本地:

$geminstallcucumber

创建好你的rails项⽬以后,编辑Gemfile加上Cucumber:

group:development,:testdo

gem"rspec-rails",">=2.0.1"

gem'cucumber-rails'

gem'databa_cleaner'

end

创建你的测试

在你的Rails项⽬⾥,Cucumber测试在features⽂件夹下⾯的.feature后缀⽂件⾥。可参考

你可以创建⼀个e⽂件写上⼀些场景(scenario):

Feature:Contactme

Inordertogetintouch

Avisitor

Shouldndmeamessagebycontactform

Scenario:Sendsacontactmessage

GivenIamonthecontactpage

AndIfillin"contactvisitorname"with"John"

AndIfillin"contactvisitoremail"with"john@"

AndIfillin"contactsubject"with"Hello"

AndIfillin"contactmessage"with"Greatpost!"

WhenIpress"Sendmessage"

Thenpageshouldhavenoticemessage"Yourmessagewassuccessfullydelivered."

*注意缩进(注:是否应该是缩进2格?上⾯代码是缩进4格的)

场景(scenario)是由很多步骤(steps)组成的。

⼀个步骤(step)是⼀个验证(validation)或者⼀个更简单的测试(simpletest).

开始运⾏cucumber命令:

$bundleexeccucumber

将得到警告说步骤没有定义:

cucumber_test$bundleexeccucumber

Usingthedefaultprofile...

Feature:Contactme

(....)

Youcanimplementstepdefinitionsforundefinedstepswiththesnippets:

Given/^Iamonthecontactpage$/do

pending#expresstheregexpabovewiththecodeyouwishyouhad

end

Given/^Ifillin"(.*?)"with"(.*?)"$/do|arg1,arg2|

pending#expresstheregexpabovewiththecodeyouwishyouhad

end

When/^Ipress"(.*?)"$/do|arg1|

pending#expresstheregexpabovewiththecodeyouwishyouhad

end

Then/^pageshouldhavenoticemessage"(.*?)"$/do|arg1|

pending#expresstheregexpabovewiththecodeyouwishyouhad

end

这⾥要⽤capybara模拟浏览器的访问。

在features/step_definitions/⽂件夹创建navigation_

_path((e(__FILE__),"..","support","paths"))

Given/^Iamon(.+)$/do|page_name|

visitpath_to(page_name)

end

When/^Igoto(.+)$/do|page_name|

visitpath_to(page_name)

end

When/^Ipress"([^"]*)"$/do|button|

click_button(button)

end

When/^Iclick"([^"]*)"$/do|link|

click_link(link)

end

When/^Ifillin"([^"]*)"with"([^"]*)"$/do|field,value|

fill_in(('','_'),:with=>value)

end

When/^Ifillin"([^"]*)"for"([^"]*)"$/do|value,field|

fill_in(('','_'),:with=>value)

end

When/^Ifillinthefollowing:$/do|fields|

_|name,value|

When%{Ifillin"#{name}"with"#{value}"}

end

end

When/^Ilect"([^"]*)"from"([^"]*)"$/do|value,field|

lect(value,:from=>field)

lect(value,:from=>field)

end

When/^Icheck"([^"]*)"$/do|field|

check(field)

end

When/^Iuncheck"([^"]*)"$/do|field|

uncheck(field)

end

When/^Ichoo"([^"]*)"$/do|field|

choo(field)

end

Then/^Ishoulde"([^"]*)"$/do|text|

have_content(text)

end

Then/^Ishoulde([^]*)$/do|regexp|

regexp=(regexp)

have_content(regexp)

end

Then/^Ishouldnote"([^"]*)"$/do|text|

_nothave_content(text)

end

Then/^Ishouldnote([^]*)$/do|regexp|

regexp=(regexp)

_nothave_content(regexp)

end

Then/^the"([^"]*)"fieldshouldcontain"([^"]*)"$/do|field,value|

find_field(field).=~/#{value}/

end

Then/^the"([^"]*)"fieldshouldnotcontain"([^"]*)"$/do|field,value|

find_field(field)._not=~/#{value}/

end

Then/^the"([^"]*)"checkboxshouldbechecked$/do|label|

find_field(label).shouldbe_checked

end

Then/^the"([^"]*)"checkboxshouldnotbechecked$/do|label|

find_field(label).should_notbe_checked

end

Then/^Ishouldbeon(.+)$/do|page_name|

current_==path_to(page_name)

end

Then/^pageshouldhave(.+)message"([^"]*)"$/do|type,text|

_css?("p.#{type}",:text=>text,:visible=>true)

end

在features/support/⽂件夹增加⼀个support⽂件

moduleNavigationHelpers

defpath_to(page_name)

capage_name

when/thehomes?page/

'/'

el

begin

page_name=~/the(.*)page/

path_components=$(/s+/)

(path_('path').join('_').to_sym)

rescueObject=>e

rai"Can'tfindmappingfrom"#{page_name}"toapath.n"+

"Now,goandaddamappingin#{__FILE__}"

end

end

end

end

World(NavigationHelpers)

再次跑⼀边测试:

$bundleexeccucumber

现在,你可以做代码⽅⾯的事了。写models,controllers,views,routes.

这⼉是我的例⼦,步骤定义的是简单的浏览(navigation),但你可以写更复杂的测试,要让场景简单,⽐如WhenIfillcontactform可以

定义成这样:

When/^Ifillcontactform$/do

visitcontact_form_path

fill_in('contact_visitor_name',:with=>"John")

fill_in('contact_visitor_email',:with=>"john@")

fill_in('contact_subject',:with=>"Hello")

fill_in('contact_message',:with=>"Greatpost!Bye.")

click_button("Sendmessage")

end

原⽂:/posts/quick-tutorial-starting-with-cucumber-and-capybara-bdd-on-rails-project/

本文发布于:2022-11-25 03:19:34,感谢您对本站的认可!

本文链接:http://www.wtabcd.cn/fanwen/fan/90/15979.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

上一篇:effection
下一篇:arrive的过去式
标签:cucumber
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图