ruby on rails 3 - How to test for non existing html tag in rspec/capybara -


for controller delivering html snippet (used ajax call), have view spec looking that:

it "should not contain html element"     render     rendered.should have_selector('div')     rendered.should_not have_selector('html') end 

since our ajax content loader doesn't allow full html page rendered, want check result not contain html tag.

for result in rendered simple div tag:

puts rendered 

leads to

"<div>some text</div>" 

however, test fails:

failure/error: rendered.should_not have_selector('html')   capybara::expectationnotmet:   expected not find css "html", found 1 match: "some text" 

i tried with

rendered.should_not have_xpath('//html') rendered.should_not have_css('html') rendered.should have_no_xpath('//html') rendered.should have_no_css('html') 

but result stays same.

why check html matching text inside div? test body leads same result.

this should not possible using capybara matchers, because capybara using nokogiri::html generate internal dom structure, , tries create valid html representation, includes html , body tag, if there none. see here in source.

if want that, fork capybara , change line like

native = nokogiri::html::fragment(native) if native.is_a?(string) 

nokogiri::html::fragment not try enhance code have completed html structure.

another solution doing string match:

rendered.match('<html').should nil 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -