-
Notifications
You must be signed in to change notification settings - Fork 5
Type Matchers
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.
Checks if a value is of a given type.
This uses the #is_a?
method.
be_a(TYPE)
be_an(TYPE)
where TYPE
is any type identifier or union of types.
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
Checks if the type of a value responds to the specified method.
respond_to(METHOD)
where METHOD
is the name of the method to check.
This can be a symbol or literal.
expect("foo").to respond_to(downcase) # true
expect(42).to respond_to(:even?) # true
expect(nil).to respond_to(:to_a) # false