ruby-on-railsregexruby-on-rails-4rspecrspec3

RSpec HtmlMatchers have_tag: can we use regular expression to match HTML attributes?


Trying to spec for a part of value of the the src attribute in an img tag rendered by one of my views, I was hoping to use the following syntax:

is_expected.to have_tag 'img', with: {src: /thumb_product\.png/}

It does not work, which is weird because the expression seems to be correctly interpreted by nokogiri (see the (?-mix:...) criteria)

expected following:
<div class='active uploaded_image'>
<img alt="Thumb product" src="/uploads/tmp/1404970363-22080-9933/thumb_product.png" />
</div>

to have at least 1 element matching "img[src='(?-mix:thumb_product\.png)']", found 0.

So I wonder if I forgot something, or if it's simply not possible. Thanks in advance


Solution

  • Manually, to test that src string ends with the expected name of the file:

    is_expected.to have_tag 'img[src$="thumb_product.png"]'