https://github.com/rspec/rspec-expectations

https://relishapp.com/rspec/rspec-expectations/v/3-7/docs

总文档连接:

RSpec.info/documentation/

be_completed是Predicate matchers

expect(actual).to be_xxx  #passes if actual.xxx?


RSpec::Expectations lets you express expected outcomes on an object in an example.

Built-in matchers

expect(actual_value).to eq(expected_value)

expect(actual_value).to be >= expected_value   包括>, < , <=, >=

expect(actual_value).to match(/expression/)  正则表达式(expect不支持匹配符号 =~)

expect(array).to all(inner_matcher)    #让array通过inner_matcher

如:expect([1,2,3]).to all(be_truthy)

expect(array).to contain_exactly(expected_array) #如果实际的和期待的数组包含相同的元素,则期待成功。不考虑顺序。

contain_exactly等同于match_array

expect(actual).to exist

Collection membership

expect(actual).to include(expected)  #actual可以是数组,字符串,hash.

例子:expect([1,2,3]).to include(1,2), expect({:a => "b"}).to include(:a => "b")

include的变型, start_with, end_with

Types/classes

expect(actual).to be_an_instance_of(expected)

# passes if actual.class == expected

expect(actual).to be_a(expected)  #passes if actual.kind_of?(expected)

Predicate matchers 

expect(actual).to be_xxx   #passes if actual.xxx?

expect(actual).to have_xxx(:arg) #passes if actual.has_xxx?(:arg)

expect(actual).to have_attributes(key/value pairs)

Truthiness

expect(actual).to be_truthy/falsy     # passes if actual is truthy (not nil or false)

expect(actual).to be true/false  #passes if actual == true/false

expect(actual).to be_nil   #passes if actual is nil

expect(actual).to_not be_nil   #passes if actual is not nil

Ranges

expect(1..10).to cover(3)

expect(actual).to be_between(min, max)

Expecting errors

expect { ... }.to raise_error
expect { ... }.to raise_error(ErrorClass)
expect { ... }.to raise_error("message")
expect { ... }.to raise_error(ErrorClass, "message")

expect可以用于block也可以用于value expectations.

Expecting throws and Yielding没看懂⚠️

Compound Matcher Expressions

通过user,or可以创建混合匹配表达式 。

expect(stoplight.color).to eq("red").or eq("green").or eq("yellow")

expect(alphabet).to start_with("a").and end_with("z")

Composing Matchers

以下matchers接受matchers 作为参数。

  • change().by(matcher) #改变sth等于by(匹配的值)
  • change{}.from(mather).to(matcher) #改变sth,从matcher_val到mather_val
  • include(matcher, matcher)
  • include(:key => matcher, :other => mathcer)
 等等:详细见https://relishapp.com/rspec/rspec-expectations/v/3-7/docs/composing-matchers

‘change’ matcher:

  • expect {do_sth}.to change(object, :attribute)
  • expect {do_sth}.to change{object.attribute}
  • 可以联合  from().to();  by(), by_at_least(), by_at_most()

Relish(n:great enjoyment; 水果蔬菜酱) 案例连接

库: rspec/rspec-expectations matcher匹配器常用的更多相关文章

  1. Matcher匹配器查找字符串指定内容

    public static void main(String[] args) { String s = "我的手机号码是18988888888,曾经用过18987654321,还用过1881 ...

  2. Rust 1.7.0 匹配器 match 的简介和使用

    使用过正則表達式的人应该都知道 matcher ,通过 matcher 匹配器运算正則表達式,完毕一系列的匹配规则. 在Rust 中 没有 switch 语句.matcher 就是 switch 的一 ...

  3. Rspec: everyday-rspec实操。FactoryBot预构件 (rspec-expectations gem 查看匹配器) 1-4章

    总文档连接: RSpec.info/documentation/ 包括core, expectiation,rails , mock, 点击最新版本,然后右上角搜索class, method. 第3章 ...

  4. Junit 断言 assertThat Hamcrest匹配器

    junit断言总结本文参考了http://blog.csdn.net/wangpeng047/article/details/9628449一 junit断言1.JUnit框架用一组assert方法封 ...

  5. 前端测试框架Jest系列教程 -- Matchers(匹配器)

    写在前面: 匹配器(Matchers)是Jest中非常重要的一个概念,它可以提供很多种方式来让你去验证你所测试的返回值,本文重点介绍几种常用的Matcher,其他的可以通过官网api文档查看. 常用的 ...

  6. Flask入门之自定义过滤器(匹配器)

    1.  动态路由的匹配器? 不知道这种叫啥名,啥用法,暂且叫做匹配器吧. Flask自带的匹配器可以说有四种吧(保守数字,就我学到的) 动态路由本身,可以传任何参数字符串或者数字,如:<user ...

  7. 【Jest】笔记二:Matchers匹配器

    一.前言 什么是匹配器? 我们可以把匹配器看成,testng断言,这么理解就可以了 二.常用的匹配器 test('two plus two is four', () => { expect(2 ...

  8. Mockito 2 参数匹配器

    Mockito 通过使用 equals() 这种自然的 Java 样式来校验参数值.有时候,当需要有其他一些灵活性的时候,你可能会要求使用参数匹配(argument matchers). 请参考下面的 ...

  9. Hamcrest匹配器框架

    其实在之前的文章中已经使用过 Hamcrest 匹配器框架,本篇文章将系统的介绍它的使用. 为什么要用Hamcrest匹配器框架 Hamcrest是一款软件测试框架, 可以通过现有的匹配器类检查代码中 ...

随机推荐

  1. C++飞机大战

    #include<windows.h> #include"resource.h" #include<stdlib.h> #include<time.h ...

  2. DIV内容垂直居中

    css垂直居中属性设置vertical-align: middle对div不起作用,例如: <!DOCTYPE html> <html lang="zh-CN"& ...

  3. Linux基础命令---ln

    ln 为指定的目录或者文件创建链接,如果没有指定链接名,那么会创建一个和源文件名字一样的链接. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fed ...

  4. ThinkPHP问题收集:模板中使用U方法时无法嵌套大括号,For标签,插入数据,新增的表字段缓存问题

    ThinkPHP模板中使用U方法时无法嵌套大括号需要在control里面用U方法赋值给变量传到模版如:{:U('/Blog/comment/',array('id'=>$id)}$comment ...

  5. QAQ的LIS树 QAQ的LIS树2 题解报告

    这两道题实际上考试的时候是一道题OwO 太可怕了,忙了我三个多小时,写了整整7K 这个题两个询问关联性不强,所以分开来考虑 QAQ的LIS树 考虑如何用dp求解答案 设dp(v)表示v到根的修改后的序 ...

  6. MySQL中的基本SQL语句

    标准SQL包含了4种基本的语句类别: DDL语句,数据定义语句,主要用来定义数据库,表名,字段,例如create,drop,alter. DML语句,数据操作语句,用来对数据记录的增删改查,还用来保证 ...

  7. ELK+Kafka学习笔记之搭建ELK+Kafka日志收集系统集群

    0x00 概述 关于如何搭建ELK部分,请参考这篇文章,https://www.cnblogs.com/JetpropelledSnake/p/9893566.html. 该篇用户为非root,使用用 ...

  8. 20145318《网络对抗》注入shellcode及Return-to-libc

    20145318<网络对抗>注入shellcode及Return-to-libc 注入shellcode 知识点 注入shellcodeShellcode实际是一段代码(也可以是填充数据) ...

  9. Xcode基本设置系列和Xcode报错解决方案

    1, arc机制中调用非arc文件. Xcode——>Project->Build Phases,将需要非arc文件更改为:"-fno-objc-arc"   ,该参数 ...

  10. P4396 [AHOI2013]作业

    题目链接 luogu4396 思路 唯有水题暖人心 咕了4天,今天跟着std对拍才做出来不得不说题解真的水的一批 先离散化一下 第一问差分询问,权值树状数组套一套就好了 \(nlog_{n}\) 第二 ...