Skip to content
Mike Miller edited this page Jan 20, 2022 · 4 revisions

These matchers operate on strings.

contain | contain_elements | have | have_elements | match | start_with | end_with | be_empty

contain

Checks that a string contains some sub-string or character.

Syntax

contain(SUBSTRING)
contain(CHARACTER)

Multiple items can be searched for by simply adding more arguments.

contain(ITEM_1, ITEM_2, ITEM_3, ..., ITEM_N)

Examples

Strings can be checked for sub-strings and characters.

expect("foobar").to contain("foo") # true
expect("foobar").to contain("baz") # false
expect("foobar").to contain('f')   # true
expect("foobar").to contain('z')   # false

And searching for multiple items to exist:

expect("fizzbuzz").to contain("fizz", "buzz") # true
expect("foobar").to contain('a', 'b', 'c')    # false

contain_elements

Checks that a string contains some set of sub-strings or characters. This behaves the same as contain, but takes an array of elements as an argument.

Syntax

contain_elements([SUBSTRING_1, SUBSTRING_2, ..., SUBSTRING_N])
contain_elements([CHARACTER_1, CHARACTER_2, ..., CHARACTER_N])

Examples

Strings can be checked for sub-strings and characters.

expect("foobar").to contain_elements(["foo", "bar"]) # true
expect("foobar").to contain_elements(["bar", "baz"]) # false
expect("foobar").to contain_elements(['f', 'o'])     # true
expect("foobar").to contain_elements(['y', 'z'])     # false

have

Checks that a string contains some sub-string or character. This is identical to contain.

have_elements

Checks that a string contains some set of sub-strings or characters. This is identical to contain_elements.

match

Checks that a string matches some regular expression. The =~ operator is used for this check.

Syntax

match(REGEX)

where REGEX is the regular expression to match against.

Examples

expect("foo").to match(/foo|bar/)  # true
expect("BAR").to match(/foo|bar/i) # true
expect("baz").to match(/foo/i)     # false

start_with

Checks that a string starts with a value.

Syntax

start_with(SUBSTRING)
start_with(CHARACTER)
start_with(REGEX)

where the argument can be a String, Char, or Regex.

Examples

expect("foobar").to start_with("foo")  # true
expect("foobar").to start_with('f')    # true
expect("FOOBAR").to start_with(/foo/i) # true
expect("foobar").to start_with("bar")  # false
expect("foobar").to start_with('b')    # false
expect("FOOBAR").to start_with(/bar/i) # false

end_with

Checks that a string ends with a value.

Syntax

end_with(SUBSTRING)
end_with(CHARACTER)
end_with(REGEX)

where the argument can be a String, Char, or Regex.

Examples

expect("foobar").to end_with("bar")  # true
expect("foobar").to end_with('r')    # true
expect("FOOBAR").to end_with(/bar/i) # true
expect("foobar").to end_with("foo")  # false
expect("foobar").to end_with('b')    # false
expect("FOOBAR").to end_with(/foo/i) # false

be_empty

Checks if a string is empty.

Syntax

be_empty

Examples

expect("foobar").to be_empty # false
expect("").to be_empty       # true