假设我有一些情况:
Feature: Creating Books
In order to have books to read
As a user
I want to create them
Background:
Given I am on the book creation page
Scenario: Creating a book
When I create the book "Moby Dick"
Then I should see "Book has been created."
和步骤定义:
Given /^I am on the ([\w\s]+)$/ do |page|
case page
when "book creation page"
visit new_book_path
else
visit page
end
end
Given /^there is a book "([\w\s]+)"$/ do |title|
steps %Q{
Given I am on the book creation page
}
fill_in 'Title', :with => 'Moby Dick'
click_button 'Create'
end
When /^I create the book "([\w\s]+)"$/ do |title|
steps %Q{
Given there is a book #{title}
}
end
运行黄瓜,我发现“鉴于有一本书”被理解为“何时”:
You can implement step definitions for undefined steps with these snippets:
When /^there is a book Moby Dick$/ do
pending # express the regexp above with the code you wish you had
end
我注意到calling steps from steps避免了跨部分语音。我是否希望做到这一点,而不是重复“当有一本书”时,“当有书时”?
答案 0 :(得分:2)
实际上我觉得你的代码是正确的,你绝对可以用这种方式调用步骤。但是你错过了步骤名称中的双引号,你试图称之为“有一本书Moby Dick”,但你所定义的步骤预计会匹配'有一本书“Moby Dick”'。如果进行以下调整:
steps %Q{
Given there is a book "#{title}"
}
应该可以正常工作。此外,对于单行步骤,您可以使用step
方法,在这种情况下可能更简洁:
step "Given there is a book \"#{title}\""