Value Matchers

Given any Ruby expression a, value matchers are expressed in the form:

 expect​(a).to matcher

To negate a matcher, use either not_to or to_not rather than to:

 expect​(a).not_to matcher
 # or
 expect​(a).to_not matcher

Equality/Identity

MatcherPasses if…Available aliases

eq(x)

a == x

an_object_eq_to(x)

eql(x)

a.eql?(x)

an_object_eql_to(x)

equal(x)

a.equal?(x)

be(x)an_object_equal_to(x)

Truthiness and Nil

MatcherPasses if…Available aliases

be_truthy

a != nil && a != false

a_truthy_value

be true

a == true

be_falsey

a == nil || a == false

be_falsy a_falsey_value a_falsy_value

be false

a == false

be_nil

a.nil?

a_nil_value

Types

MatcherPasses if…Available aliases

be_an_instance_of(klass)

a.class == klass

be_instance_of(klass) an_instance_of(klass) ...

Get Effective Testing with RSpec 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.