Reputation: 275
Can somebody explain my why this test can't be passed? I'm simply test article view with html content. In view i display content through html_safe
.article_content
= @article.content.html_safe
in my test i have something like below:
context 'shows actual article' do
before(:all) { @article = FactoryGirl.build(:article, id: 1,
content: '<h2>aaaaa ddd bbb</h2>') }
before(:each) { render }
it 'show content' do
render.should have_selector('div.article_content',text: @article.content.html_safe)
end
after that my test fails:
Failure/Error: render.should have_selector('div.article_content',text: @article.content.html_safe)
Capybara::ExpectationNotMet:
expected to find css "div.article_content" with text "<h2>aaaaa ddd bbb</h2>"
but there were no matches. Also found "\naaaaa ddd bbb\n", which matched the selector but not all filters.
but when i drop html tags from factory object's content test pass. i don't know why html tags are chane to '\n'. in browser all looks good.
Upvotes: 2
Views: 1577
Reputation: 649
After struggling a lot with the same issue I've came to following solution:
Include this at the bottom of your spec
File:
def strip_tags(string)
ActionController::Base.helpers.strip_tags(string)
end
Now you can test for things like this:
expect(page).to have_content(strip_tags(I18n.t('my_text.applicant_html')))
Now even if your translation includes some html tags it will check correctly.
Upvotes: 2
Reputation: 1929
The has_selector/have_selector matcher matches on the text which is actually visible - and the text that shows up on the page is aaaaa ddd bbb
rather than <h2>aaaaa ddd bbb</h2>'
Upvotes: 1