-
Notifications
You must be signed in to change notification settings - Fork 5
String Matchers
These matchers operate on strings.
contain | contain_elements | have | have_elements | match | start_with | end_with | be_empty
Checks that a string contains some sub-string or character.
contain(SUBSTRING)
contain(CHARACTER)
Multiple items can be searched for by simply adding more arguments.
contain(ITEM_1, ITEM_2, ITEM_3, ..., ITEM_N)
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
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.
contain_elements([SUBSTRING_1, SUBSTRING_2, ..., SUBSTRING_N])
contain_elements([CHARACTER_1, CHARACTER_2, ..., CHARACTER_N])
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
Checks that a string contains some sub-string or character. This is identical to contain.
Checks that a string contains some set of sub-strings or characters. This is identical to contain_elements.
Checks that a string matches some regular expression.
The =~
operator is used for this check.
match(REGEX)
where REGEX
is the regular expression to match against.
expect("foo").to match(/foo|bar/) # true
expect("BAR").to match(/foo|bar/i) # true
expect("baz").to match(/foo/i) # false
Checks that a string starts with a value.
start_with(SUBSTRING)
start_with(CHARACTER)
start_with(REGEX)
where the argument can be a String
, Char
, or Regex
.
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
Checks that a string ends with a value.
end_with(SUBSTRING)
end_with(CHARACTER)
end_with(REGEX)
where the argument can be a String
, Char
, or Regex
.
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
Checks if a string is empty.
be_empty
expect("foobar").to be_empty # false
expect("").to be_empty # true