黄瓜,Rails:has_content?即使字符串不存在也通过测试

时间:2019-10-30 13:45:44

标签: ruby-on-rails cucumber capybara

场景是:

    Scenario: View welcome page
    Given I am on the home page
    Then I should see 'Welcome'

该步骤的定义是

Then("I should see {string}") do |string|
  page.has_content?(string)
end

该测试通过,无论“欢迎”一词是否出现在主页中。我在做什么错了?

2 个答案:

答案 0 :(得分:1)

只有抛出异常,步骤才会失败。按照其命名约定,如果内容不在页面中,则has_content?方法将返回false,因此不会引发异常。如果您打算失败,这将导致您的步骤“通过”。

您需要使用某种单元测试库进行断言(我的Ruby有点生锈)

Then("I should see {string}") do |string|
  page.has_content?(string).should_be true
end

您需要RSpec之类的东西才能访问允许您进行断言的库。

答案 1 :(得分:0)

以其他答案中所示的方式执行此操作将起作用,但不会给出有用的错误消息。相反,您想要

对于RSpec

expect(page).to have_content(string)

用于迷你测试

assert_content(string)

对于其他人

page.assert_content(string)

请注意,assert_content / assert_text和have_content / have_text是彼此的别名,因此请使用阅读效果更好的那个。

相关问题