1. describe和context
describe和context方法用来组织相关的行为example。
使用一个字符串作为他们的参数,以及使用一个block来定义其上下文的范围。
写model的spec或者其他的unit test时,可以传一个Ruby类作为describe的第一个参数。Doing so also creates an implicit subject for the examples.
describe Timesheet do
……
end
describe可以嵌套
一般使用:
describe Timesheet do
describe “#test” do
…..
end
end
这样的方式(注意第一个describe的参数是一个类,第二个describe参数是以#开始)这个表示测试Timesheet类下面的test方法)
2. let(:name) {expression}
let方法简单的使用后面的block创建memoized attributes.换句话说就是为后面的测试准备数据,跟before里的代码一样的,但是比before里的代码效果更好。
memozied的意思是let后面关联的block只执行一次,然后会缓存该变量,提高执行效率。
Lazy,有需要才会运算,并且是Memoized
相较于before(:each)增加执行速度
不需要用instance variable放before
增加可读性
let!则是非lazy
3. before和after
和setup、teardown方法类似
Before and after code can be inserted in any describe or context blocks, and by default the execute for each it block that shares their scope.
4. it
it方法使用一个描述和block。一个it就是一个测试,最好一个it一个期望
As mentioned, the idea is to complete the thought that was started in the describe method, so that it foms a complete sentence.
5. specify
specify是it方法的别名,但是他可以使用不同的结构来增加可读性。
describe BlogPost do
set(:blog_post) {blog_post = BlogPost.new}
specify {blog_post.should_not be_published}
end
生成的RSpecDoc如下:
BlogPost
- should not be published
6. expect
expect 用来改变一个值或者抛出一个异常。后面接change来表示要达到的值,使用raise_error(异常类)来表示会抛出一个异常。
expect {
BlogPost.create :title => “Hello”
}.to change {BlogPost.count}.by(1)
希望在expect块里做完之后,BlogPost.count的值要改为1
改变值的例子
describe Order do
let(:order) {order = Order.create}
describe "#ship!” do
context “with paid” do
it "should update status to shipping" do
expect {
order.ship!
}.to change { order.status }.from(“new”).to(“ship”)
end
end
end
这里改变值使用的from和to,这样就会在执行expect块之前检查order.status的值是不是new,并且会在执行之后检查是不是”ship”值
抛出异常的例子
describe Order do
let(:order) {order = Order.create}
describe "#ship!” do
context “with paid” do
it "should raise NotPaidError" do
expect {
order.paid? = flase
order.ship!
}.to raise_error(NotPaidError)
end
end
end
这里表示执行完except块之后会抛出一个NotPaidError异常。
7. pending
可以使用pending来列出打算要写的测试
使用it函数不传block给他也是pending的意思,也可以在block离调用pending
可以在before里写pending
8. should和should_not
Rspec mixes them into the base Ruby Object class at runtime so that they are available on all objects.They expect to receive Matcher objects, generated using Rspec expectation syntax
receiver.should be_true
receiver.should be_false
receiver.should be_nil
检查型别、方法
receiver.should be_a_kind_of(Array)
receiver.should be_an_instance_of(Array)
receiver.should responsed_to(:foo)
检查Array、Hash
receiver.should have_key(:foo)
receiver.should include(4)
receiver.should have(3).items
任何be_开头
receiver.should be_empty
receiver.should be_blank
receiver.should be_admin
should == 是万能的
Rspec的Matcher很多,也可以自己写
9. Implicit Subject和Explicit Subject
使用subject可省略receiver
10. its
its可以省略receiver的方法调用
describe Order do
subject { Order.new}
its(:status) {should == “New”}
end
11.
分享到:
相关推荐
请查看以下文章,了解新的RSpec语法和RSpec 3中的更改:如果要在升级到RSpec 3的过程中使用Transpec,请阅读RSpec官方指南:例子这是一个示例规范: describe Account do subject ( :account ) { Account ....
原版The RSpec Book并附有源码
rspec出入门者学习理解,介绍的非常的详细、清楚; 欢迎下载学习~
rspec 下载请注意:英文文档
rspec_api_documentation, 从RSpec自动生成API文档 RSpec Doc为你的Rails API生成漂亮的。查看一个示例文件。更改请查看维基以了解最新的更改。安装将rspec_api_documentation添加到你的文件gem 'rspec_a
jruby-1.5.5+OperaWatir+RSpec
使用RSpec 测试Rails 程序.pdf
rspec-api-blueprint-formatter, 从RSpec测试自动生成API文档 ! RSpec APIBlueprint格式化程序从RSpec测试自动生成API文档 !像这样it 'retrievs the patients medications' do retrieve_medications
使用RSpec测试Rails程序。本书基于Rails 4.1,使用完整应用 来演示TDD的流程。
rspec-collection_matchers, 集合基数匹配器,从rspec期望中提取 RSpec::CollectionMatchers RSpec::CollectionMatchers 让你在一个例子中表达一个对象集合的预期结果。expect(account.shopping_cart).to have_
RSpec.Essentials 英文
ruby(rails)单元测试相关的gem,rspec、cucumeber。BDD
rspec 是目前bdd測試的使用工具,很適合描述測試的內容
rspec_junit_formatter, RSpec结果格式化为你的CI可以读取的JUnit RSpec JUnit格式化程序 RSpec 2 & 3结果, Jenkins可以读取。 可能还有其他的CI服务。灵感来自于的工作,在的RSpec格式化程序在对 Reporter的失望...
RSpec :: Wait允许您使用已经知道和喜欢的RSpec语法糖来等待声明通过。 RSpec :: Wait将继续尝试,直到您的断言通过或超时为止。 例子 RSpec :: Wait的wait_for断言几乎替代了RSpec的expect断言。 主要区别在于...
RSpec和Minitest匹配器来预防N 1查询问题
Api-rspec_api_documentation.zip,从rspecrspec api doc generator自动生成api文档,一个api可以被认为是多个软件设备之间通信的指导手册。例如,api可用于web应用程序之间的数据库通信。通过提取实现并将数据放弃到...
rspec_demo Rspec演示
db-query-matchers, 用于数据库查询的RSpec匹配器 db-query-matchers 用于数据库查询的RSpec匹配器。安装在你的应用程序中添加这一行,最好在你的test 组中:gem 'db-query-matchers'然后执行:bundle