O'Reilly logo

Effective Testing with RSpec 3 by Myron Marston, Ian Dees

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required