0

I am trying to get an RSpec integration test to fail.

Given the following HTML fragment:

<article id="content">  
  <section>    
    <p>There aren't any travel promotions... yet!</p>
  </section>
</article>

When I run the following Rspec test:

describe SomeController do
  render_views

  describe "GET 'promotion_index'" do
    it "should display an empty page given a blank page fragment and no promotions " do
      get :promotion_index
      response.should have_selector("#content section:first-of-type", :content => "")
    end
  end
end

Then the test should fail

But it doesn't. It passes beautifully whether or not there is content in the selector.

Just to be clear, I don't want to test that the <p> content is not present. I want to test that <article id="content"><section /></article> contains no content at all.

Chris Bloom
  • 3,526
  • 1
  • 33
  • 47

1 Answers1

0

Are you talking about making sure there are no child tags or there is no text in the middle? I don't use RSpec's have_selector method. I also couldnt' get it to work. I just use Nokogiri. You just pass in the rendered object into Nokogiri::HTML(rendered) like this and just parse the HTML to test. It's quite simple.

Give it a try.

html = Nokogiri::HTML(rendered)
html.xpath("//[@id='content']/section").each do |node|
 node.children_should be_empty?
end

Something like that. I'm not exactly sure.

Allen
  • 794
  • 1
  • 6
  • 19
  • I want to make sure that there is no markup in the section tag - neither content nor HTML. Can you give me an example of a cucumber step that uses Nokogiri to do this? It might be worth a shot if Capybara can't do it natively. – Chris Bloom Jun 08 '12 at 03:39