kucaahbe / rspec-html-matchers

Old school have_tag, with_tag(and more) matchers for rspec 3 (Nokogiri powered)
http://rubygems.org/gems/rspec-html-matchers
MIT License
199 stars 90 forks source link

link_to tag with text and content_tag fails when trying to match text #30

Closed chanko closed 10 years ago

chanko commented 10 years ago

This is my code

def filter_tag
  link_to('#', id: 'delete-filter', class: "btn") do
    ('Test' + content_tag(:span, 'x', class: 'tag-close')).html_safe
  end
end

This is my test

expect(filter_tag).to have_tag :a, with: { class: 'btn', id: 'delete-filter', href: '#' } do
  with_text 'Test'
  with_tag :span, text: 'x', with: { class: 'tag-close' }
end

And this is the error I get when running this test

"Test" expected within "a.btn[id='delete-filter'][href='#']" in following template:
<a class="btn" href="#" id="delete-filter">Text<span class="tag-close">x</span></a>

If I remove the with_text line and only leave the with_tag, my test passes. And if I remove the content_tag from the actual code, the test passes with just with_text. There's something about the combination of both that breaks the test.

kucaahbe commented 10 years ago

Hello @chanko , with_text matches all text with tag, in your case that will be "Testx" (with "x" from span element), did you try with_text /Test/?

chanko commented 10 years ago

I had just started looking at the code and saw that with_text matches all the text in the tag and in any tag within it.

I didn't know I could match regex. That works and my tests are passing now.

Thanks @kucaahbe! :thumbsup:

kucaahbe commented 10 years ago

You're welcome @chanko :)