黄瓜测试无法呈现整页,只有标题

时间:2017-11-29 15:29:59

标签: ruby-on-rails testing rspec cucumber capybara

我对Cucumber / BDD很新,我有点难过。我正在尝试对页面内容进行简单的测试,但似乎我的测试套件只能“找到”页面标题。

例如:

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页面上作为导航栏中的已注销用户时。这正是这种情况。但我无法弄清楚为什么它无法读取整个页面的内容。

1 个答案:

答案 0 :(得分:0)

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

这样的东西
<html>
  <head>...</head>
  <body>
    <header>...</header>
  <body>
  <body> You're actual page content </body>
</html>

由于只允许一个<body>元素,因此忽略第二个元素。修复您的HTML有效(将<header>移到主<body>内)并解决您的问题