2012-02-29 24 views
0

比方说,我有一些场景,如:是否可以从黄瓜的When步中调用给定步骤?

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避免了言语的交叉部分。我是否希望做到这一点,而不是将'有书'复制到'有书时'?

回答

2

其实我认为你的代码是正确的,你绝对可以用这种方式调用步骤。然而,你已经错过了步骤名称中的双引号,你试图打电话'有一本书莫比迪克',但你定义的步骤预计匹配'有一本书“莫比迪克”。如果你做出以下调整:

steps %Q{ 
    Given there is a book "#{title}" 
} 

它应该工作正常。此外,对于一个行一步,你可以使用step方法,这可能是在这种情况下更简洁:

step "Given there is a book \"#{title}\"" 
+0

这是它到底。 – troutwine 2012-03-01 04:14:21