Michael Durrant
Michael Durrant

Reputation: 96454

Compare string to regex in rspec?

I was doing

expect(@link.url_address == 'abc').to be_true

but url_address might have other text after abc so I am trying

expect(@link.url_address =~ 'abc').to be_true

but I am getting

Failure/Error: expect(@link.url_address =~ /abc/).to be_true
   expected  to respond to `true?`

I also tried

expect(@link.url_address).to =~ /abc/

but I get

 Failure/Error: expect(@link.url_address).to =~ /abc/
 ArgumentError:
   The expect syntax does not support operator matchers, so you must pass a matcher to `#to`.

Upvotes: 17

Views: 22105

Answers (1)

Surya
Surya

Reputation: 15992

Try this:

expect(@link.url_address).to match(/abc/)

Source: https://github.com/rspec/rspec-expectations#regular-expressions

Upvotes: 47

Related Questions