检查 RSpec 中选择器的多个副本

发布于 2024-12-14 07:49:59 字数 768 浏览 0 评论 0原文

我在 RSpec 中的一项测试遇到问题。有问题的测试应该通过检查页面上是否存在多个

标签来测试索引页上是否有多个帖子。我当前的实现如下所示:

it "should have more than one post" do
  get :index
  response.should have_selector("article", :class => "post", :count => 2)
end

由于某种原因,尽管在调试输出中显示

标记的多个实例,但此测试失败。它给出的具体错误是

Failures:

1) PostsController GET 'index' should have more than one posts
   Failure/Error: response.should have_selector("article", :class => "post", :count => 2)
   expected following output to contain a <article class='post'/> tag:

请注意,如果我删除 :count =>; 2 选项测试通过,没有发生任何事件。

有谁知道为什么这个测试失败?

I'm having a problem with one of my tests in RSpec. The test in question is supposed to test whether there is more than one post on the index page, by checking for the existence of more than one <article class="post"> tag on the page. My current implementation looks like this:

it "should have more than one post" do
  get :index
  response.should have_selector("article", :class => "post", :count => 2)
end

For some reason this test fails, despite showing multiple instances of the <article class="post"> tag in the debug output. The specific error it give is

Failures:

1) PostsController GET 'index' should have more than one posts
   Failure/Error: response.should have_selector("article", :class => "post", :count => 2)
   expected following output to contain a <article class='post'/> tag:

Note that if I remove the :count => 2 option the test passes without incident.

Does anyone know why this test is failing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒗幽 2024-12-21 07:49:59

我猜你在 RSpec 下使用 Capybara 或者 Webrat?使用 count 选项,您的测试断言响应恰好包含 2 个帖子,而不是至少 2 个。最小选项是否满足您的要求?

请参阅 https://github.com/jnicklas/capybara/ blob/master/lib/capybara/node/matchers.rbhttp://rubydoc.info/github/jnicklas/capybara /master/Capybara/Node/Matchers#has_selector%3F-instance_method

I'm guessing you are using Capybara under RSpec here or Webrat? With the count option your test is asserting that the response contains exactly 2 posts, rather than at least 2. Does the minimum option do what you want?

See https://github.com/jnicklas/capybara/blob/master/lib/capybara/node/matchers.rb and http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers#has_selector%3F-instance_method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文