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 个定义与最后一个定义不明确。但是对于开始和结束日期,我必须以不同的方式处理它们。我所知道的是 cucumber 具有解决该问题的 --guess 选项。但这是最佳实践吗?

4

2 回答 2

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

(或者,在文本框中输入了类似的内容,因此不确定它是否会按原样正确运行。)

于 2012-05-22T04:32:34.697 回答
0

去掉围绕“开始于”和“结束于”的引号。它们将不再符合您的最后一步。我发现引号会引起很多歧义,因此我通常只将它们用于文件名或其中包含空格的路径之类的东西。如有疑问,我发现删除引号是最佳做法,因为它消除了 Cucumber 的大量猜测。

于 2012-05-22T13:16:11.843 回答