博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[RSpec] LEVEL 2 CONFIGURATION & MATCHERS
阅读量:4668 次
发布时间:2019-06-09

本文共 3589 字,大约阅读时间需要 11 分钟。

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  endend

Answer:

describe Zombie do  it 'includes a tweet' do    tweet = Tweet.new    zombie = Zombie.new(tweets: [tweet])    zombie.tweets.should include tweet  endend

 

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  endend

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)  endend#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  endend

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: trueend
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  endend

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      )  endend

 

More matchers:

转载于:https://www.cnblogs.com/Answer1215/p/4098397.html

你可能感兴趣的文章
oracle 11g r2安装
查看>>
关于自关联1
查看>>
存储控制器、MMU、flash控制器介绍
查看>>
hdu-1814(2-sat)
查看>>
自我反省
查看>>
反射,得到Type引用的三种方式
查看>>
pl sql练习(2)
查看>>
Problem B: 判断回文字符串
查看>>
谷歌浏览器,添加默认搜索引擎的搜索地址
查看>>
数据结构化与保存
查看>>
C# .net 获取程序运行的路径的几种方法
查看>>
为什么需要Docker?
查看>>
国内5家云服务厂商 HTTPS 安全性测试横向对比
查看>>
how to control project
查看>>
转 python新手容易犯的6个错误
查看>>
第四节 -- 列表
查看>>
Python入门学习笔记4:他人的博客及他人的学习思路
查看>>
webstorm里直接调用命令行
查看>>
关联规则算法之FP growth算法
查看>>
对数组序列进行洗牌
查看>>