0

我试图让 RSpec 集成测试失败。

给定以下 HTML 片段:

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

当我运行以下 Rspec 测试时:

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

那么测试应该失败

但事实并非如此。无论选择器中是否有内容,它都会很好地传递。

为了清楚起见,我不想测试<p>内容不存在。我想测试<article id="content"><section /></article>根本不包含任何内容。

4

1 回答 1

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.

于 2012-06-07T23:59:12.177 回答