黄瓜表中的动态数据

KJF*_*KJF 19 bdd cucumber

我有一个Cucumber表,其中一个字段是我希望填充今天日期的日期.有没有办法做到这一点,而无需硬编码今天的日期到表中?

基本上我想进入Time.now.strftime("%Y-%m-%d")桌子而不是让它休息.

Bra*_*nar 21

由于表的步骤定义正在处理,您可以在表中放置一个特殊的占位符,例如字符串"TODAYS_DATE",然后用于map_column!将列中的数据处理为您想要的格式.

例如,给出下表

Given the following user records
  | username | date        |
  | alice    | 2001-01-01  |
  | bob      | TODAYS_DATE |
Run Code Online (Sandbox Code Playgroud)

在您的步骤定义中,您将拥有

Given /^the following user records$/ do |table|
  table.map_column!('date') do |date| 
    if date == 'TODAYS_DATE'
      date = Time.now.strftime("%Y-%m-%d")
    end
    date
  end
  table.hashes.each do |hash|
    #Whatever you need to do
  end
end
Run Code Online (Sandbox Code Playgroud)

请注意,这只会在您请求哈希时更改值.table和table.raw将保持不变,但只要你需要行哈希,它们就会被map_column中的代码转换!


Gan*_*kar 7

我知道自问这个问题已经很久了,但我最近和Cucumber做了类似的事情,所以如果有人感兴趣,这里有另一种解决方案......

Given the following user records
 | username | date                             |
 | bob      | Time.now.strftime("%Y-%m-%d")    |
Run Code Online (Sandbox Code Playgroud)

然后在你的步骤定义中只需eval()日期字符串

Given /^the following user records$/ do |table|
  table.hashes.each do |hash|
    date = eval(hash["date"])
  end
end
Run Code Online (Sandbox Code Playgroud)

虽然不像布兰登的例子,但是如果没有进一步的逻辑,这也不会让你输入确切的日期.