[RSpec] LEVEL 2 CONFIGURATION & MATCHERS
Installing RSpec
In this level we'll start by getting you setup on a regular Ruby project, then move onto using RSpec within Rails. Let's start by installing the rspec gem from the console.
gem install rspec

Command Line
With the RSpec gem installed, you will have access to the command line tool, rspec.
RSpec has a few settings you can configure, so to help us get started, let's initialize this as an RSpec project. This will generate a placeholder for our RSpec configuration.
rspec --init
Rails Configuration
Using rspec --init will setup RSpec within a ruby project, but for the rest of this course we'll be using RSpec within a Rails project. Run the rails generator to install RSpec into the current Rails project.
rails generate rspec:install
Running specs from the command line
We now have a Rails project all setup and we've created spec/models/zombie_spec.rb for you. Run this spec from the command line with color on, and specify documentation format.
rspec --color --format documentation spec/models/zombie_spec.rb
Predicate Matchers
Refactor the following spec to use an include matcher.
class Zombie < ActiveRecord::Base
validates :name, presence: true def genius?
iq >= 3
end
end
Answer:
describe Zombie do
it 'includes a tweet' do
tweet = Tweet.new
zombie = Zombie.new(tweets: [tweet]) zombie.tweets.should include tweet end
end
Change Matcher
In the following example, we're checking to see that a method changes the state of a zombie. We need to make sure the zombie was in a specific state before and after the method is called.
Refactor the following example to use the expect and change syntax.
class Zombie < ActiveRecord::Base
validates :name, presence: true
validates :iq, numericality: true def eat_brains
self.iq += 3
end
end
Answer:
describe Zombie do
it 'gains 3 IQ points by eating brains' do
zombie = Zombie.new
#zombie.iq.should == 0
#zombie.eat_brains
#zombie.iq.should == 3
expect {zombie.eat_brains}.to change {zombie.iq}.from(0).to(3)
end
end #what expect {zombie.eat_brains}.to change {zombie.iq}.from(0).to(3)
#is saying that:
#zombie eat_brains
#then change zombie.iq
#from 0 to 3 # format:
#expect {action}.to change {value}.by(num) #or form(num1).to(num2)

Have Matcher
We're verifying the count to be greater than 0, but we really could be using a have matcher here to verify that the zombie has exactly one tweet. Refactor the spec to use the have matcher.
describe Zombie do
it 'increases the number of tweets' do
zombie = Zombie.new(name: 'Ash')
zombie.tweets.new(message: "Arrrgggggggghhhhh")
#zombie.tweets.count.should > 0
zombie.should have(1).tweets
end
end

Raises an Error
Testing for exceptions is tricky business. Refactor the spec below to use the raise_error matcher with an expect block.
class Tweet < ActiveRecord::Base
attr_accessible :message
belongs_to :zombie
validates :message, presence: true
end
class Zombie < ActiveRecord::Base
validates :name, presence: true class NotSmartEnoughError < StandardError; end def genius?
iq >= 3
end def make_decision!
raise NotSmartEnoughError unless genius?
return true
end
end
Answer:
describe Zombie do
it 'raises a Zombie::NotSmartEnoughError if not able to make a decision' do
zombie = Zombie.new
#begin
# zombie.make_decision!
# rescue Zombie::NotSmartEnoughError => e
# e.should be_an_instance_of(Zombie::NotSmartEnoughError)
# end
expect {zombie.make_decision!}.to raise_error(
Zombie::NotSmartEnoughError
)
end
end

More matchers:

[RSpec] LEVEL 2 CONFIGURATION & MATCHERS的更多相关文章
- [RSpec] LEVEL 1: INTRODUCTION
Install RSpec: Describe Lets start writing a specification for the Tweet class. Write a describe blo ...
- Understanding JDBC Internals & Timeout Configuration
原版:http://www.cubrid.org/blog/dev-platform/understanding-jdbc-internals-and-timeout-configuration 中文 ...
- Spring boot参考指南
介绍 转载自:https://www.gitbook.com/book/qbgbook/spring-boot-reference-guide-zh/details 带目录浏览地址:http://ww ...
- @Async in Spring--转
原文地址:http://www.baeldung.com/spring-async 1. Overview In this article we’ll explore the asynchronous ...
- 日志框架只打印出Mybatis SQL的配置
项目比较大,各种乱七八糟的框架.Log4j配置的是INFO级别. 然而今天开发的时候我需要log4j打印出SQL的执行情况. 先改log4j的rootLogger级别到DEBUG......后果就是各 ...
- 一键式Spring集成工具 Spring Boot
最近公司使用Spring boot进行开发,稍微了解一下,不过自我感觉把集中式配置applicate.properties搞明白,注解用过Spring MVC的boot绝对没问题的 比如拦截器:@As ...
- Logback常用配置详解
logback是一套日志框架,由log4j的优化版,由同一个作者开发,在速度和性能上都超过其他日志框架,再结合slf4j,已成为当前最流行的日志框架. Logback最常用就是在classpath定义 ...
- java_log_02
配置 在第一部分,我们将介绍配置 logback 的各种方法,给出了很多配置脚本例子.在第二部分,我们将介绍 Joran,它是一个通用配置框架,你可以在自己的项目里使用 Joran 一.Logback ...
- Spring Boot Logback应用日志
e Spring Boot Logback应用日志 2015-09-08 19:57 7673人阅读 评论(0) 收藏 举报 . 分类: Spring Boot(51) . 目录(?)[+] 日志对于 ...
随机推荐
- TCP拥塞控制及连接管理
在阅读此篇之前,博主强烈建议先看看TCP可靠传输及流量控制. 一.TCP拥塞控制 在某段时间,若对网络中某资源的需求超过了该资源所能提供的可用部分,网络的性能就要变坏——产生拥塞(congestion ...
- 【Python】单例模式Singleton
前两天一个面试被问到python中单例模式有几种实现方式,只答出了可以用元类实现...然后就想不起来了. 之后翻书,原来这些之前都见过的啊.... 1.手动实现真正创建实例的方法__new__()来实 ...
- 更改paramiko 源码 记录命令实现堡垒机功能
利用paramiko 下的demo可以很容易的实现记录客户在操作客户机时的命令,修改\demos\interactive.py def posix_shell(chan): import select ...
- Python 正则表达式中级
首先是?: 在括号中用?:用在findall和split之中,去除括号优先级. 如果不用只输出括号内匹配的值 r 的作用是转义python里面换行符等,像是\n 不用加\来转义 1.子表达式 ...
- [2]树的DFS序
定义: 树的DFS序就是在对树进行DFS的时候,对树的节点进行重新编号:DFS序有一个很强的性质: 一颗子树的所有节点在DFS序内是连续的一段, 利用这个性质我们可以解决很多问题. 代码: void ...
- 「SCOI2016」美味
「SCOI2016」美味 题目描述 一家餐厅有 \(n\) 道菜,编号 \(1 \ldots n\) ,大家对第 \(i\) 道菜的评价值为 \(a_i \:( 1 \leq i \leq n )\) ...
- vagrant 常用命令以及常用操作
列出这些命令,主要是防止脑内存不足.目前这些命令是我常用的,以后其他命令用的多,我再继续添加... 分享些本人用的百度网盘box,国外的太坑... 本人分享的百度网盘:http://pan.baidu ...
- 内功心法 -- java.util.LinkedList<E> (7)
写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------下文主要对java.util ...
- C# MATLAB混合编程
我附带把matlab配置过程也给大家上传上来.[转载]终于学会C#调用matlab函数了,原来这么简单(也可以下载附件查看)自己的配置: (1)Microsoft Visual Studio 2005 ...
- uboot启动内核的实现
前面我们分析了uboot 的整个流程,我们知道uboot启动以后所有功能都是通过命令来实现的,启动kernel就是执行了bootcmd里面的命令.命令执行过程在uboot中是非常重要的现在我们就来看u ...