[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) . 目录(?)[+] 日志对于 ...
随机推荐
- Java反射机制Reflection
Java反射机制 1 .class文件 2 Class类 3 Class类与反射机制 4 Java反射机制的类库支持及简介 5 反射机制的定义与应用 6 反射机制Demo Java反射机制demo(一 ...
- MySQL之varchar
MySQL之varchar 0.前言 探究关于MySQL中varchar长度的定义 1.研究MySQL的varchar列类型的背景 1.MySQL5.x 2.InnoDB 2.varchar 对于va ...
- mySQL 的 2个分类
1.将如下表中的每门成绩都大于80分的人名? 张三 语文 81 张三 数学 75 李四 语文 76 李四 数学 90 王五 语文 81 王五 数学 100 王五 英语 90 select * from ...
- hdu 3949 XOR 线性基 第k小异或和
题目链接 题意 给定\(n\)个数,对其每一个子集计算异或和,求第\(k\)小的异或和. 思路 先求得线性基. 同上题,转化为求其线性基的子集的第k小异或和. 结论 记\(n\)个数的线性基为向量组\ ...
- px,dp,sp以及像素密度
px px(pixel): 像素,是指在由一个数字序列表示的图像中的一个最小单位.在Android中,无论屏幕密度多少,一个像素单位对应一个屏幕像素单位,不会根据屏幕密度自动缩放,因此一般不推荐使用p ...
- hdu 3046 最小割
每个栅栏其实就是一条边,修一些栅栏,使得狼不能抓到羊,其实就是求一个割,使得羊全在S中,狼全在T中. #include <cstdio> #include <cstring> ...
- asp.net url传值,弹窗
一,<a>标签链接式传值 1, <a href="News_list.aspx?ClassID=<%#((DataRowView)Container.DataItem ...
- HDU 5305 Friends dfs
Friends 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5305 Description There are n people and m pa ...
- ios/object-c中的UIColor一些比较偏的颜色展示/示例
UIColor blackColor]; [UIColor darkGrayColor]; [UIColor lightGrayColor]; [UIColor grayColor]; [UIColo ...
- Jquery实现可拖拽的树菜单
效果图例如以下所看到的:下载地址http://download.csdn.net/detail/javaquentin/8290417 <html xmlns="http://www. ...