2012-05-22 52 views
1

我是新來的黃瓜,我想詢問我如何可以組織以下步驟定義一個建議,手柄黃瓜曖昧步驟

Feature: Manage events 
    In order to Manage events 
    Organizer 
    wants to create new events 

    Background: 
    Given I am logged in as organizer 

    Scenario: Publish new event 
    Given I am on the new event page 
    When I fill in "Title" with "Rails event" 
    And I fill in "Start at" with "2012-5-4" 
    And I fill in "End at" with "2012-5-5" 
    And I fill in "Description" with "Bla bla" 
    And I check "Published" 
    And I press "Create Event" 
    Then I should see "Rails event" 

下面是創建歧義步驟定義,

When /^I fill in "Start at" with "(.*?)-(.*?)-(.*?)"$/ do |year, month, day| 
     enter_date(:event_start_at, year, month, day) 
    end 

    When /^I fill in "End at" with "(.*?)-(.*?)-(.*?)"$/ do |year, month, day| 
     enter_date(:event_end_at, year, month, day) 
    end 

    When /^I fill in "(.*?)" with "(.*?)"$/ do |name, value| 
     fill_in name, :with => value 
    end 

    private 
    def enter_date(id, year, month, day) 
     select year, :with => "#{id}_1i" 
     select month, :with => "#{id}_2i" 
     select day, :with => "#{id}_3i" 
    end 

會發生什麼情況是前2個定義與最後一個定義不一致。但是對於開始和結束日期,我必須以不同的方式處理它們。 我知道的是,黃瓜有解決這個問題的--guess選項。但這是最佳做法嗎?

回答

2

爲什麼不這樣做:

When /^I fill in "([^"]*)" with "([^"]*)"$/ do |name, value| 
    case(name) 
    when "Start at" then 
    parts = value.split(/-/) 
    enter_date(:event_start_at, parts[0], parts[1], parts[2]) 
    when "End at" then 
    parts = value.split(/-/) 
    enter_date(:event_end_at, parts[0], parts[1], parts[2]) 
    else 
    file_in name, :with => value 
    end 
end 

(或者類似的規定,輸入到文本框所以不知道是否會正常運行原樣)

0

擺脫引用「開始於」和「結束於」。他們將不再符合你的最後一步。我發現引號引起了很多不明確的地方,所以我通常只將它們用於諸如文件名或路徑中的空格之類的東西。如果有疑問,我發現刪除引號是最好的做法,因爲它爲黃瓜消除了很多猜測。