无论页面内容如何,​​Cucumber/capybara 功能始终会通过

发布于 2025-01-04 16:06:59 字数 1368 浏览 1 评论 0原文

我有以下黄瓜场景:

Background:
  Given the following roles exist:
  | id | name    |
  | 1  | janitor |
  And the following users exist:
  | username | password   | email               | role_ids |
  | janitor  | Cleaning31 | [email protected] | 1        |
  And I am logged in as "janitor" with password "Cleaning31"
  And the following articles exist:
  | id | title       | slug        | content      |
  | 1  | Article One | article-one | Article one. |

Scenario: Seeing link for deleting an article
  When I view the article list
  Then I should see the "Delete" link for article 1

......以及最后一步的定义:

Then /^I should (not )?see the "([^"]*)" link (?:in|under|for) article (\d+)$/ do |negation, content, article_id|
  page.should have_selector("\#article_#{article_id}") do |article|
    if negation
      article.should_not have_css('a', text: content)
    else
      article.should have_css('a', text: content)
    end
  end
end

即使我在功能或视图中将“删除”一词更改为“这永远不会起作用”,该功能总是会通过。然而,在浏览器中,“删除”链接仅正确显示在具有“看门人”角色的用户的文章上。

Cucumber 的“显示页面”和“显示响应”似乎没有被定义,尽管大量文章建议它们应该被定义。 (我的 env.rb

其他功能似乎按预期工作。有人能发现我哪里出了问题吗?

I have the following cucumber scenario:

Background:
  Given the following roles exist:
  | id | name    |
  | 1  | janitor |
  And the following users exist:
  | username | password   | email               | role_ids |
  | janitor  | Cleaning31 | [email protected] | 1        |
  And I am logged in as "janitor" with password "Cleaning31"
  And the following articles exist:
  | id | title       | slug        | content      |
  | 1  | Article One | article-one | Article one. |

Scenario: Seeing link for deleting an article
  When I view the article list
  Then I should see the "Delete" link for article 1

...and the definition for that last step:

Then /^I should (not )?see the "([^"]*)" link (?:in|under|for) article (\d+)$/ do |negation, content, article_id|
  page.should have_selector("\#article_#{article_id}") do |article|
    if negation
      article.should_not have_css('a', text: content)
    else
      article.should have_css('a', text: content)
    end
  end
end

Even if I change the word "Delete" to "this should never work" in either the feature or the view, the feature always passes. In the browser, however, the "Delete" link correctly only appears on articles for users who have the "janitor" role.

Cucumber's "show me the page" and "show me the response" don't seem to be defined, despite a large number of articles suggesting they should be. (My env.rb)

Other feature seem to work as expected. Can anybody spot where I'm going wrong with this?

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

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

发布评论

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

评论(1

迷你仙 2025-01-11 16:06:59

我在你的测试中没有看到任何断言。尝试将 article.should_not have_css('a', text: content) 更改为 assertarticle.should_not have_css('a', text: content),依此类推。

编辑:

结果是什么?

element = page.find("\#article_#{article_id}")
if negation
      element.should_not have_css('a', text: content)
else
      element.should have_css('a', text: content)
end

恕我直言,您应该使用查找助手,然后对您找到的元素进行断言。我想知道您在该块中有什么可用的“文章”。

I see no assertions in your test. Try changing article.should_not have_css('a', text: content) to assert article.should_not have_css('a', text: content), and so on.

Edit:

What's the result of?

element = page.find("\#article_#{article_id}")
if negation
      element.should_not have_css('a', text: content)
else
      element.should have_css('a', text: content)
end

Imho you should use find helper and then make assertions on the element you find. I wonder what you have available in the block as "article".

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