引用链接:http://reverocean.iteye.com/blog/1489957

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

Rspec基本语法的更多相关文章

  1. 【转】iOS开发工具系列(按功能分)

    http://www.cocoachina.com/newbie/basic/2014/0417/8187.html 这是我们多篇iOS开发工具系列篇中的一篇,此前的文章比如:那些不能错过的Xcode ...

  2. iOS 开发常用的一些工具

    http://www.itjhwd.com/ios-tool/ 通用工具 HomeBrew:OS X上非常出色的包管理工具. 源码控制 Git:分布式版本控制系统和源码管理系统,其优点是:快和简单易用 ...

  3. 32款iOS开发插件和工具介绍[效率]

    插件和工具介绍内容均收集于网络,太多了就不一一注明了,在此谢过!   1.Charles 为了调试与server端的网络通讯协议.经常须要截取网络封包来分析. Charles通过将自己设置成系统的网络 ...

  4. Selenium WebDriver + Grid2 + RSpec之旅(一)----准备篇

    Selenium WebDriver + Grid2 + RSpec之旅(一)             ----准备篇 前言 在Web 2.0 应用中,在浏览器种类盛行的时代,我们在测试过程中不仅要模 ...

  5. Rspec: everyday-rspec实操。FactoryBot预构件 (rspec-expectations gem 查看匹配器) 1-4章

    总文档连接: RSpec.info/documentation/ 包括core, expectiation,rails , mock, 点击最新版本,然后右上角搜索class, method. 第3章 ...

  6. Ruby之Rspec的报错解决

    #enconding:utf-8 require 'selenium-webdriver' require 'rspec' describe "baidu main page" d ...

  7. 我的MYSQL学习心得(一) 简单语法

    我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...

  8. Swift与C#的基础语法比较

    背景: 这两天不小心看了一下Swift的基础语法,感觉既然看了,还是写一下笔记,留个痕迹~ 总体而言,感觉Swift是一种前后端多种语言混合的产物~~~ 做为一名.NET阵营人士,少少多多总喜欢通过对 ...

  9. 探索C#之6.0语法糖剖析

    阅读目录: 自动属性默认初始化 自动只读属性默认初始化 表达式为主体的函数 表达式为主体的属性(赋值) 静态类导入 Null条件运算符 字符串格式化 索引初始化 异常过滤器when catch和fin ...

随机推荐

  1. 电脑当路由使用(目前只在win7上用过)

    前提:电脑有无线网卡,并打开了无线 第一步使用管理员权限运行cmd.exe 1.执行如下命令 netsh wlan set hostednetwork mode=allow ssid=myWifi k ...

  2. C基础题-sizeof

    sizeof  C语言中判断数据类型或者表达式长度符:关键字:字节数的计算在程序编译时进行,而不是在程序执行的过程中才计算出来! 一.关于sizeof简单的总结 1.sizeof的使用形式:sizeo ...

  3. [51nod1384]全排列

    法一:next_permutation函数,两个参数分别为起始指针和末尾指针. #include<bits/stdc++.h> using namespace std; typedef l ...

  4. Express的日志模块morgan

    morgan 是nodejs的一个日志模块,由 express 团队维护. 这里通过示例简要介绍morgan模块在express中的应用,大部分示例直接来自于.morgan的文档:https://gi ...

  5. mysql数据库int(5)以及varchar(20)长度表示的是什么?

    在mysql5.x版本的数据库中: int类型数据的字节大小是固定的4个字节: 但是int(5)和int(11)区别在于,显示的数据位数一个是5位一个是11位,在开启zerofill(填充零)情况下, ...

  6. CentOS 6.6 Oracle 安装

    阿里云服务器上要装Oracle,搞了半天才搞定. 项目阿里云用的是CentOS 6.5 X86_64 ,我本地虚拟机装的是CentOS 6.6 X86_64.不过用 cat /proc/version ...

  7. GridView 高亮某一行

    <script type="text/javascript"> $(document).ready(function () { $("#GridView tr ...

  8. C# Collection was modified;enumeration operation may not execute

    一.问题描述 在做 数组.列表.集合遍历时,可能我们会遇见这个问题.Collection was modified;enumeration operation may not execute ,翻译的 ...

  9. Linux中关机和磁盘管理命令

    常用的关机命令 shutdown -h 关机 -r 重启 halt poweroff reboot 重启 logout 退出登录命令 磁盘管理命令 df -h 以1024进制计算最合适的单位显示磁盘容 ...

  10. How to generate rtabmap with a Realsense D435 or Xtion Pro Live?(如何使用Realsense D435或者Xtion Pro Live生成rtabmap?)

    Ubuntu16.04,ROS kinetic 1.在ROS中安装rtabmap_ros包 sudo apt-get install ros-kinetic-rtabmap-ros 2. RGB-D相 ...