Module: RSpec::ExpectationHelper::Shared

Includes:
Matchi::Helper
Included in:
It, Its
Defined in:
lib/r_spec/expectation_helper/shared.rb

Overview

Abstract expectation helper base module.

This module defines a number of methods to create expectations, which are automatically included into examples.

It also includes a collection of expectation matchers 🤹

Examples:

Equivalence matcher

matcher = eql("foo") # => Matchi::Matcher::Eql.new("foo")
matcher.matches? { "foo" } # => true
matcher.matches? { "bar" } # => false

matcher = eq("foo") # => Matchi::Matcher::Eq.new("foo")
matcher.matches? { "foo" } # => true
matcher.matches? { "bar" } # => false

Identity matcher

object = "foo"

matcher = equal(object) # => Matchi::Matcher::Equal.new(object)
matcher.matches? { object } # => true
matcher.matches? { "foo" } # => false

matcher = be(object) # => Matchi::Matcher::Be.new(object)
matcher.matches? { object } # => true
matcher.matches? { "foo" } # => false

Regular expressions matcher

matcher = match(/^foo$/) # => Matchi::Matcher::Match.new(/^foo$/)
matcher.matches? { "foo" } # => true
matcher.matches? { "bar" } # => false

Expecting errors matcher

matcher = raise_exception(NameError) # => Matchi::Matcher::RaiseException.new(NameError)
matcher.matches? { Boom } # => true
matcher.matches? { true } # => false

Truth matcher

matcher = be_true # => Matchi::Matcher::BeTrue.new
matcher.matches? { true } # => true
matcher.matches? { false } # => false
matcher.matches? { nil } # => false
matcher.matches? { 4 } # => false

Untruth matcher

matcher = be_false # => Matchi::Matcher::BeFalse.new
matcher.matches? { false } # => true
matcher.matches? { true } # => false
matcher.matches? { nil } # => false
matcher.matches? { 4 } # => false

Nil matcher

matcher = be_nil # => Matchi::Matcher::BeNil.new
matcher.matches? { nil } # => true
matcher.matches? { false } # => false
matcher.matches? { true } # => false
matcher.matches? { 4 } # => false

Type/class matcher

matcher = be_instance_of(String) # => Matchi::Matcher::BeInstanceOf.new(String)
matcher.matches? { "foo" } # => true
matcher.matches? { 4 } # => false

matcher = be_an_instance_of(String) # => Matchi::Matcher::BeAnInstanceOf.new(String)
matcher.matches? { "foo" } # => true
matcher.matches? { 4 } # => false

See Also: