0

我有一个表单,我使用 page-object gem 填充了 populate_page_with data_for 。它是这样定义的:

def add_fruit(data = {})
  populate_page_with data_for(:new_fruit, data)
  add_fruit_submit
end

然后我以这种方式在我的步骤中调用该方法:

on(AddFruitPage).add_fruit

我的 yml 文件如下所示:

new_fruit:
  color: red
  size: large
  type: apple
  price: 0.75
  ...
another_fruit
  color: orange
  size: medium
  type: orange
  price: 0.99
  ...

我知道我可以通过在我的步骤中执行以下操作来覆盖每个字段:

When(^/I add a banana$/) do
  on(AddFruitPage).add_fruit('color' => 'yellow', 'size' => 'small', 'type' => 'banana')
end

由于另一个水果的数据已经在 yml 文件中,我可以使用参数告诉方法要加载哪些数据,而不是在使用方法时指定每个值吗?就像是:

def add_fruit(data = {})
  if(data['type'] == 'another')
    populate_page_with data_for(:new_fruit, data)
  else
    populate_page_with data_for(:another_fruit, data)
  end
end

并这样称呼它?

on(AddFruitPage).add_fruit('type' => 'another')

Type 是一个可选参数,仅用于加载另一组数据。颜色、大小、类型和价格都映射到在类中定义的页面上的文本字段。有可能做这样的事情吗?

4

1 回答 1

0

如果您使用的是 Ruby 2,则可以使用命名参数 - 为水果类型创建一个命名参数,其余的作为数据数组的一部分。

而不是使用数据中已经存在的“类型”,我可能会使用一个参数来指定data_for. 方法定义很简单:

def add_fruit(fruit_type: :new_fruit, **data)
  populate_page_with data_for(fruit_type, data)
  add_fruit_submit
end

可以通过多种方式调用:

add_fruit()  # Specify nothing
add_fruit(:color => 'red')  # Just specify the data
add_fruit(:fruit_type => :another_fruit)  # Just specify the fruit type
add_fruit(:fruit_type => :another_fruit, :color => 'red')  # Specify fruit type and data

如果你使用的是 Ruby 2 之前的版本,你可以这样做:

def add_fruit(data = {})
  fruit_type = data.delete(:fruit_type) || :new_fruit
  populate_page_with data_for(fruit_type, data)
  add_fruit_submit
end
于 2014-01-21T14:57:30.343 回答