Cucumber 测试无法呈现整页,只能呈现 header

Cucumber test can't render full page, only header

我是 Cucumber/BDD 的新手,有点难过。我正在尝试对页面内容进行简单测试,但似乎我的测试套件只能 "find" 页面 header.

例如:

Scenario: User sees the menu page
  Given a menu exists
  When I visit the menu
  Then I should see "menu" page title
  And I should see "tuna poke" item name

步骤定义:

Given("a menu exists") do
  user = User.create!(email: "test@valid.email", password: "hello")
  restaurant = Restaurant.create!(name: "eleven tables", user_id: user.id)
  menu = Menu.create!(restaurant_id: restaurant.id)
  category = Category.create!(name: "appetizer")
  menu_item = MenuItem.create!(name: "tuna poke", description: "it's light", price: 9, category: category, menu: menu)
end

When("I visit the menu") do
  visit restaurant_menu_path
end

Then("I should see {string} page title") do |string|
  expect(page).to have_title "#{string}"
end

And("I should see {string} item name") do |item_name|
  expect(page).to have_content "#{item_name}"
end

给我结果:

Feature: View Menu
  In order see the menu
  I want to view the menu
  I want to see item name
  I want to see item description
  I want to see item price

  Scenario: User sees the menu page        # features/view_menu.feature:8
    Given a menu exists                    # features/step_definitions/view_menu_steps.rb:1
    When I visit the menu                  # features/step_definitions/view_menu_steps.rb:9
    Then I should see "menu" page title    # features/step_definitions/view_menu_steps.rb:13
    And I should see "tuna poke" item name # features/step_definitions/view_menu_steps.rb:17
      expected to find text "tuna poke" in "menu sign in" (RSpec::Expectations::ExpectationNotMetError)
      ./features/step_definitions/view_menu_steps.rb:18:in `"I should see {string} item name"'
      features/view_menu.feature:12:in `And I should see "tuna poke" item name'

Failing Scenarios:
cucumber features/view_menu.feature:8 # Scenario: User sees the menu page

1 scenario (1 failed)
4 steps (1 failed, 3 passed)
0m1.180s

我觉得最奇怪的是这部分错误:

expected to find text "tuna poke" in "menu sign in"

这对我来说意义不大。这三个词唯一一次同时出现是当您在我的应用程序的 /menu 页面上作为导航栏中的 logged-out 用户时。这正是这里的情况。但是我想不通为什么它不能读取整个页面的内容。

<html> 元素允许包含一个 <head> 元素后跟一个 <body> 元素的内容。在您的情况下,您在 <body> 元素之外有一个 <header> 元素。这会导致您使用的任何驱动程序(我假设 rack_test 因为它最宽松)在 <header> 元素周围生成一个隐含的 <body> 元素,所以您最终会得到类似

<html>
  <head>...</head>
  <body>
    <header>...</header>
  <body>
  <body> You're actual page content </body>
</html>

由于只允许一个 <body> 元素,因此第二个将被忽略。修复您的 HTML 使其有效(将 <header> 移到主 <body> 中)它应该可以解决您的问题