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

Cucumber, Rails: has_content? test passes even though the string isn't there

场景是:

    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

测试是否通过 "welcome" 这个词是否在首页。我做错了什么?

一个步骤只有在抛出异常时才会失败。根据其命名约定,如果内容不在页面中,has_content? 方法 returns false,因此不会抛出异常。当您打算失败时,这将导致您的步骤 "pass"。

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

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

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

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

对于RSpec

expect(page).to have_content(string)

对于迷你测试

assert_content(string)

对于其他人

page.assert_content(string)

请注意 assert_content/assert_text 和 have_content/have_text 是彼此的别名,因此请使用更好的别名。