[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) . 目录(?)[+] 日志对于 ...
随机推荐
- 隐马尔可夫模型(Hidden Markov Model)
隐马尔可夫模型(Hidden Markov Model) 隐马尔可夫模型(Hidden Markov Model, HMM)是一个重要的机器学习模型.直观地说,它可以解决一类这样的问题:有某样事物存在 ...
- [BZOJ5109][LOJ #6252][P4061][CodePlus 2017 11月赛]大吉大利,今晚吃鸡!(最短路+拓扑排序+传递闭包+map+bitset(hash+压位))
5109: [CodePlus 2017]大吉大利,晚上吃鸡! Time Limit: 30 Sec Memory Limit: 1024 MBSubmit: 107 Solved: 57[Sub ...
- sqlserver -- 学习笔记(二)“SQL Server 阻止了对组件 'xp_cmdshell' 的 过程'sys.xp_cmdshell' 的访问”解决方法
将数据表导出到excel时出现下面错误: SQL Server 阻止了对组件 'xp_cmdshell' 的 过程'sys.xp_cmdshell' 的访问,因为此组件已作为此服务器安全配置的一部分而 ...
- 关于图表第三方Charts的一些理解与总结
最近项目中用到了很多的图表,如柱状图,线状图,饼状图等等.接触到了一个新的第三方Charts,在做图方面确实非常强大,在使用了一段时间后,今天对他进行一个小的总结,也是自己的一点小理解. 关于char ...
- Vue学习记录-画页面
webstorm 因为之前开发ReactNative的时候,选择了webstorm,这回转战Vue,自然还是用它.如果什么也不做的话,打开Vue工程,编辑区域基本上没有语法高亮.怎么办呢? 安装插件( ...
- HAproxy + keepalived 实现双机热备
一.HAProxy简介: HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点, ...
- 破解ZendStudio 10.1
破解文件的网盘地址: http://pan.baidu.com/share/link?shareid=3562282358&uk=1543766223
- django 获取 POST 请求值的几种方法(转)
转载请注明出处:http://hi.baidu.com/leejun_2005/blog/item/9a37a22238f35c5bac34de54.html from:http://stackove ...
- Windows Embedded Compact 7网络编程概述(下)
11.1.1 Select I/O模型 在Windows CE中,Select模型是唯一被支持的I/O模型.Select I/O模型就是利用select函数对I/O进行管理. 函数select的功能在 ...
- wget 下载文件重进行命名
wget在下载的时候就重命名的: wget -c "www.baidu.com" -O baidu.index.html 保存输出日至,可以使用: wget -c "ww ...