Skip to content
Arctic Fox edited this page May 3, 2019 · 1 revision

Type matchers inspect a value's type and methods it responds to. Union types are also supported.

be_a | respond_to

be_a

Checks if a value is of a given type. This uses the #is_a? method.

Syntax

be_a(TYPE)
be_an(TYPE)

where TYPE is any type identifier or union of types.

Examples

expect("foo").to be_a(String)       # true
expect(42).to be_an(Int32)          # true
expect(true).to be_a(Nil)           # false
expect(:a).to be_a(String | Symbol) # true

respond_to

Checks if the type of a value responds to the specified method.

Syntax

respond_to(METHOD)

where METHOD is the name of the method to check. This can be a symbol or literal.

Examples

expect("foo").to respond_to(downcase) # true
expect(42).to respond_to(:even?)      # true
expect(nil).to respond_to(:to_a)      # false
Clone this wiki locally