rspec 测试里面可以使用的方法有什么要求

JackSongBlack 的BLOG
用户名:JackSongBlack
文章数:56
评论数:10
访问量:23220
注册日期:
阅读量:5863
阅读量:12276
阅读量:351359
阅读量:1049742
51CTO推荐博文
本文主要介绍怎么使用Rspec+factory_girl进行复杂模型测试,首先介绍下本人使用的模型机构class Nodebelongs_to :parent,:class_name =&Nodehas_many :children,:class_name =&Node &&&&&&&&&&&&&&&& :foreign_key =&:parent_id endend可以看出,我使用的是一个自关联表,通过自己:parent_id这个键将本表自己关联起来,现在介绍怎么用factory_girl模拟这样的模拟结构首先介绍从叶子结点像root结点一个一对一的模型结构代码如下FactoryGirl.define dofactory :node dotitle "XXXXX"factory :node_leaf ,:class =& :node doassociation :parent,:factory =&:nodeendend通过association这个值,我们将node与node_leaf做成一对一关联然后我们再构建root结点向leaf结点一个一对多的情况FactoryGirl.define dofactory :node dotitle "XXXXX"factory :node_root,:class =& :node doafter_create do |node|node.children &&FactoryGirl.create(:node,:parent =& node)node.children &&FactoryGirl.create(:node,:parent =& node)node.children &&FactoryGirl.create(:node,:parent =& node)endendendend然后我们再下Rspec代码中创建测试模型Factory.build(:node_root) #这种方式不会被保存在数据库中Factory.create(:node_leaf)#这种方式其实就多了一个SAVE动作如果想查找可以下一结点用关键字查询比如Factory.create(:node_root).children.find_by_title("1")也可以使用:each这个选项遍历整个模型比如Factory.create(:node_root).children.each do |node|node.titleend关于一对多的关系&factory_girl&测试&很不错关于factory_girl的介绍,很全面&
了这篇文章
类别:未分类┆阅读(0)┆评论(0)RSPEC-入门学习 - tim_sheng - 博客园
随笔 - 73, 文章 - 1, 评论 - 16, 引用 - 0
&&&&& 要了解RSpec,我们首先需要了解什么是行为驱动开发(Behaviour Driven Development,简称BDD),BDD是一种融合了可接受性测试驱动计划(Acceptance Test Driven Planning),域驱动设计(Domain Driven Design)以及测试驱动开发(Test Driven Development,简称TDD)的敏捷开发模型。RSpec为BDD开发提供TDD支持。
&&&&& 这篇文章遵从TDD思想,但是我们将使用行为(behavior)和样例(example)来代替测试例(test case)和测试方法(test method)。
& textual descriptions of examples and groups ()
& flexible and customizable reporting
& extensible expectation language ()
built-in mocking/stubbing framework ()
&&&&& rails new rspec_tutional &T&&&&&&&
&&&&& 加上-T,创建工程时不会添加测试内容,然后修改Gemfile,添加如下内容
group :test, :development do
gem &rspec&
gem &rspec-rails&(两者版本最好一致)
&然后通过下面的命令创建rspec框架
&&&&& rails g respec:install
&&&&& 此命令的作用是为Rails程序安装RSpec框架,使RSpec取代Test::Unit而存在,这条命令会建立&.rspec&文件、&spec&目录和&spec/spec_helper.rb&文件,他们分别的作用是:
&& &.rspec&文件:存放运行rake spec任务时,RSpec需要加载的配置信息。
&& &spec&目录:我们的RSpec测试用例的存放空间,针对mvc,我们可以建立models、views和controllers目录分别存放模型、视图和控制器各个层面的测试用例,每一个测试文件以_spec结尾。
&& &spec/spec_helper.rb&文件:运行测试用例的RSpec配置文件。每一个以_spec结尾的文件都需要引入该文件,即在文件开头添加:require &spec_helper&代码。
describe User do& it "should be in any roles assigned to it" do&&& user = User.new&&& user.assign_role("assigned role")&&& user.should be_in_role("assigned role")& end& it &should NOT be in any roles not assigned to it& do&&& user = User.new&&& user.should_not be_in_role(&unassigned role&)& endend
class User& def in_role?(role)&&& role == @role& end& def assign_role(role)&&& @role = role& endend
Expectations--- Built-in matchers
Equivalence
actual.should eq(expected) # passes if actual == expected
actual.should == expected # passes if actual == expected
actual.should eql(expected) # passes if actual.eql?(expected)
actual.should equal(expected) # passes if actual.equal?(expected)
actual.should === expected # passes if actual === expected
actual.should be(expected) # passes if actual.equal?(expected)
Comparisons
actual.should be & expected
actual.should be &= expected
actual.should be &= expected
actual.should be & expected
actual.should be_within(delta).of(expected)
Regular expressions
actual.should =~ /expression/
actual.should match(/expression/)
Types/classes
actual.should be_an_instance_of(expected)
actual.should be_a_kind_of(expected)
Truthiness
actual.should be_true # passes if actual is truthy (not nil or false)
actual.should be_false # passes if actual is falsy (nil or false)
actual.should be_nil # passes if actual is nil
Expecting errors
expect { ... }.to raise_error
expect { ... }.to raise_error(ErrorClass)
expect { ... }.to raise_error("message")
expect { ... }.to raise_error(ErrorClass, "message")
Expecting throws
expect { ... }.to throw_symbol
expect { ... }.to throw_symbol(:symbol)
expect { ... }.to throw_symbol(:symbol, 'value')
Predicate matchers
actual.should be_xxx # passes if actual.xxx?
actual.should have_xxx(:arg) # passes if actual.has_xxx?(:arg)
Ranges (Ruby &= 1.9 only)
(1..10).should cover(3)
Collection membership
actual.should include(expected)
[1,2,3].should include(1)
[1,2,3].should include(1, 2)
{:a =& 'b'}.should include(:a =& 'b')
"this string".should include("is str")
上边是一个比较完整的体现RSpec组织测试用例的模板,当然这里只是为了说明一个_spec文件如何组织测试用例及基本用法,所以我没有去编写每一个测试用例的体,并且例子出现的字符串内容不针对于具体的系统,没有实际意义。下边依次解释含义:
&&&&& require &spec_helper&:目的是加载&spec/spec_helper.rb&文件中的RSpec配置,运行时需要。
describe()方法:
&&&&& 我们用describe()方法定义一个测试用例组,describe()方法的参数可以是我们要测试的对象(如例中的People),可以是一个字符串,用来描述该组,describe方法后的字符串所描述的内容可以对应一个用户故事。
&&&&& 注意describe()方法是可以嵌套的,两个describe()方法衔接起来可以更细化一个用户故事,如上边的里面的describe()方法内就表示:&People have enough money pay for house&。
it()方法:
&&&&& 我们用it()方法定义一个具体的测试用例(在RSpec中,称一个测试用例为一个example)。其后的字符串为该方法的参数,用来描述一个具体的场景,it方法体就是我们对系统在该场景下的行为的定义。
&&&&& It()方法和describe()方法衔接起来,可以完整的描述一个系统行为,以上边的最后的一个测试用例为:&People have enough money pay for house should travel &。
context()方法:
&&&&& Context()方法和describe()方法的作用一样,不同点在于describe倾向于描述我们的测试对象,而context()方法倾向于用字符串描述用户故事。
before()和after():
&&&&& 这两个方法很多测试框架都支持,需要说明的是这两个方法的参数,例子中为符号&:each&,表明对于每一个测试用例,先执行 before()方法中的代码,用例完成后执行after()方法中的代码,若参数为&:all&,则表示在所有的测试用例执行之前,先执行 before()方法中的代码,所有的用例完成后执行after()方法中的代码。
&&&&& RSpec还提供around()方法,暂不懂其用法,之后的报告中补上。
帮助方法:
&&&&& 所谓的帮助方法就是把多个测试用例中重复的操作抽出作为一个公用的方法,提高代码重用度。如例子中的work_hard()方法。
共享行为:
&&&&& 系统的某一个行为是很多场景下都具有的,那么我们可以把它定义为一个共享行为,我们通过share_examples_for()方法 定义共享行为,使用it_behaves_like()方法共享定义过的系统行为,如例子中的share_examples_for &any people&, it_behaves_like &any people&。
pending()方法:
&&&&& 我们确定系统拥有一个行为,但是还没有具体定义,这时我们可以将该行为使用pending()方法来设置该行为为待定义,其后的字符串参数将在生成的报告中显示。
&&&&& pending()方法的另外一种用法就是,当一个测试用例失败时,我们可以利用pending方法设置其为一个待修复的bug,方法体内包含使用例失败的代码。例如最后一个测试用例的内容君,已阅读到文档的结尾了呢~~
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
RSpec 让你爱上写测试
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口【图文】Rails Rspec测试(内部培训资料)_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
评价文档:
Rails Rspec测试(内部培训资料)
上传于||文档简介
&&软​件​测​试​简​单​介​绍​
​
​R​a​i​l​s​ ​测​试​
​
​R​s​p​e​c​ ​/​ ​R​s​p​e​c​ ​R​a​i​l​s​
​
​M​o​c​k​ ​S​t​u​b
大小:1.63MB
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢

我要回帖

更多关于 rspec expect 的文章

 

随机推荐