我可以检查我的HTML代码段只有一个顶级节点吗?

时间:2015-01-07 17:32:01

标签: ruby-on-rails-3 rspec capybara

我正在测试一个帮助方法,并将它生成的HTML片段转换为Capybara节点(node = Capybara::Node::Simple.new(html)

我可以检查HTML是否只包含我期望的顶级节点吗?也就是说,如果我希望我的HTML格式为:

<div class="foo">
  <div class="part-a">A</div>
  <div class="part-b">B</div>
</div>

...如何验证该节点没有兄弟姐妹的顶级节点(div.foo),

1 个答案:

答案 0 :(得分:0)

经过一番摆弄后,我把它解决了。 Capybara::Node::Simple.new()将代码段包装在简单的<html><body>对中。这意味着以下工作:

# Check that the body has only one direct child
expect(node).to have_css('body > *', count: 1)

# Check that the expected node exists (and hang on to it for subsequent testing)
foo = node.first('body > div.foo')
expect(foo).not_to be_nil
相关问题