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的更多相关文章

  1. [RSpec] LEVEL 1: INTRODUCTION

    Install RSpec: Describe Lets start writing a specification for the Tweet class. Write a describe blo ...

  2. Understanding JDBC Internals & Timeout Configuration

    原版:http://www.cubrid.org/blog/dev-platform/understanding-jdbc-internals-and-timeout-configuration 中文 ...

  3. Spring boot参考指南

    介绍 转载自:https://www.gitbook.com/book/qbgbook/spring-boot-reference-guide-zh/details 带目录浏览地址:http://ww ...

  4. @Async in Spring--转

    原文地址:http://www.baeldung.com/spring-async 1. Overview In this article we’ll explore the asynchronous ...

  5. 日志框架只打印出Mybatis SQL的配置

    项目比较大,各种乱七八糟的框架.Log4j配置的是INFO级别. 然而今天开发的时候我需要log4j打印出SQL的执行情况. 先改log4j的rootLogger级别到DEBUG......后果就是各 ...

  6. 一键式Spring集成工具 Spring Boot

    最近公司使用Spring boot进行开发,稍微了解一下,不过自我感觉把集中式配置applicate.properties搞明白,注解用过Spring MVC的boot绝对没问题的 比如拦截器:@As ...

  7. Logback常用配置详解

    logback是一套日志框架,由log4j的优化版,由同一个作者开发,在速度和性能上都超过其他日志框架,再结合slf4j,已成为当前最流行的日志框架. Logback最常用就是在classpath定义 ...

  8. java_log_02

    配置 在第一部分,我们将介绍配置 logback 的各种方法,给出了很多配置脚本例子.在第二部分,我们将介绍 Joran,它是一个通用配置框架,你可以在自己的项目里使用 Joran 一.Logback ...

  9. Spring Boot Logback应用日志

    e Spring Boot Logback应用日志 2015-09-08 19:57 7673人阅读 评论(0) 收藏 举报 . 分类: Spring Boot(51) . 目录(?)[+] 日志对于 ...

随机推荐

  1. mycat数据库中间件入门

    首先从官网上下载mycat. 官网链接 下载对应的mycat即可. 我也是小白一个,就是直接在window上操作了. 自己画的,真low. 推荐一篇文章 https://blog.csdn.net/u ...

  2. 「APIO2018新家」

    「APIO2018新家」 题目描述 五福街是一条笔直的道路,这条道路可以看成一个数轴,街上每个建筑物的坐标都可以用一个整数来表示.小明是一位时光旅行者,他知道在这条街上,在过去现在和未来共有 \(n\ ...

  3. Codeforces 1037 H. Security

    \(>Codeforces \space 1037\ H. Security<\) 题目大意 : 有一个串 \(S\) ,\(q\) 组询问,每一次给出一个询问串 \(T\) 和一个区间 ...

  4. 安卓中AsyncTask的基本使用

    安卓中AsyncTask的基本使用 使用场景介绍 在安卓开发中,我们经常需要访问互联网资源,这些访问是都需要在后台线程中去完成的,因为安卓的UI线程不允许执行耗时任务.然而,后台线程是不可以修改安卓的 ...

  5. java_es

    在查询时过滤掉指定的"_id"的数据 GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();// 对 ...

  6. mybatis源码分析(4)----org.apache.ibatis.binding包

    1.  我们通过接口操作数据库时,发现相关的操作都是在org.apache.ibatis.binding下 从sqSessin 获取getMapper() SqlSession session = s ...

  7. delphi tcp/ip IdTCPServer1实例一

    unit Unit1; interface uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Form ...

  8. Spark调研笔记第4篇 - PySpark Internals

    事实上.有两个名为PySpark的概念.一个是指Sparkclient内置的pyspark脚本.而还有一个是指Spark Python API中的名为pyspark的package. 本文仅仅对第1个 ...

  9. hihoCoder - 1082 - 然而沼跃鱼早就看穿了一切 (字符串处理!!)

    #1082 : 然而沼跃鱼早就看穿了一切 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描写叙述 fjxmlhx每天都在被沼跃鱼刷屏,因此他急切的找到了你希望你写一个程序屏蔽全 ...

  10. C#:让您知道您的方法是被何“人”调用

    我们要在DisabledObsoleteMethod函数里限制具有“Obsolete”属性的方法调用,我们如何去做呢?在.Net中提供了一个"StackFrame"类用于表示当前线 ...