1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
Feature: match matcher
The match matcher calls #match on the object, passing if #match returns a
truthy (not false or nil) value. Regexp and String both provide a #match
method.
```ruby
"a string".should match(/str/) # passes
"a string".should match(/foo/) # fails
/foo/.should match("food") # passes
/foo/.should match("drinks") # fails
```
This is equivalent to using the =~ matcher (see the operator matchers
feature for more details).
Scenario: string usage
Given a file named "string_match_spec.rb" with:
"""ruby
describe "a string" do
it { should match(/str/) }
it { should_not match(/foo/) }
# deliberate failures
it { should_not match(/str/) }
it { should match(/foo/) }
end
"""
When I run `rspec string_match_spec.rb`
Then the output should contain all of these:
| 4 examples, 2 failures |
| expected "a string" not to match /str/ |
| expected "a string" to match /foo/ |
Scenario: regular expression usage
Given a file named "regexp_match_spec.rb" with:
"""ruby
describe /foo/ do
it { should match("food") }
it { should_not match("drinks") }
# deliberate failures
it { should_not match("food") }
it { should match("drinks") }
end
"""
When I run `rspec regexp_match_spec.rb`
Then the output should contain all of these:
| 4 examples, 2 failures |
| expected /foo/ not to match "food" |
| expected /foo/ to match "drinks" |
|