【发布时间】:2017-11-29 15:29:58
【问题描述】:
我对 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 页面上时。这正是这里的情况。但是我不知道为什么它不能读取整个页面的内容。
【问题讨论】:
-
输出
page.html看看页面上的实际内容 -
那个输出是正确的:jsfiddle.net/x98724tb
标签: ruby-on-rails testing rspec cucumber capybara