Cucumber 表中的动态数据

发布于 2024-08-09 00:18:39 字数 197 浏览 2 评论 0原文

我有一个 Cucumber 表,其中一个字段是我想用今天的日期填充的日期。 有没有一种方法可以做到这一点,而不必将今天的日期硬编码到表中?

基本上我想在表中输入 Time.now.strftime("%Y-%m-%d") 并且不让它中断。

I have a Cucumber table, one of the fields is a date which I would like to have populated with todays date.
Is there a way of doing this without having to hard code todays date into the table?

Basically I would like to enter Time.now.strftime("%Y-%m-%d") into the table and not have it break.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

今天小雨转甜 2024-08-16 00:18:39

由于该表正在由您的步骤定义进行处理,因此您可以在表中放置一个特殊的占位符,例如字符串“TODAYS_DATE”,然后使用 map_column! 处理该列中的数据以你想要的格式。

例如,给出下表,

Given the following user records
  | username | date        |
  | alice    | 2001-01-01  |
  | bob      | TODAYS_DATE |

在您的步骤定义中,您将

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

注意到,这仅在您要求哈希时更改值。 table 和 table.raw 将保持不变,但每当您需要行哈希时,它们将由 map_column 中的代码进行转换!

Since the table is being processed by your step definition, you could put a special place holder in the table, such as the string "TODAYS_DATE", and then use map_column! to process the data in the column to the format you want.

For example given the following table

Given the following user records
  | username | date        |
  | alice    | 2001-01-01  |
  | bob      | TODAYS_DATE |

In your step definition you would have

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

Note this only changes the values when you ask for the hash. table and table.raw will remain the same, but whenever you need the row hashes, they will be converted by the code within the map_column!

谈场末日恋爱 2024-08-16 00:18:39

我知道自从提出这个问题以来已经有很长时间了,但我最近正在做一些与 Cucumber 类似的事情,所以如果有人感兴趣的话,这里有一个替代解决方案...

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

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

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

虽然与 Brandon 的示例不同,但这不会让你也输入了准确的日期,没有任何进一步的逻辑。

I know it's been ages since this question was asked but I was doing something similar with Cucumber recently so here's an alternative solution if anyone's interested...

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

And then in your step definition just eval() the date string

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

Though unlike Brandon's example this wont let you put in exact dates as well without some further logic.

沦落红尘 2024-08-16 00:18:39

如果您想做的话,bodnarbm 的答案非常好。我自己的建议是看看 timecop 宝石。用它来设置已知日期的时间,然后相应地调整您的表格。

bodnarbm's answer is pretty good if that is what you want to do. My own suggestion would be to take a look at the timecop gem. Use it to set time to a known day then adjust your tables accordingly.

并安 2024-08-16 00:18:39

基于固定装置文件,我创建了以下代码:

功能:

Given the following "Inquirers":
  | id | email                    | start_date        |
  |  1 | [email protected] | <%= Time.now %>   |

帮助程序:

Given(/^the following "(.*?)":$/) do |model, table|
  table.hashes.each do |hash|
    attributes = Rack::Utils.parse_nested_query(hash.to_query)
    object     = model_name.classify.constantize.new

    attributes.keys.each do |key|
      object.send("#{key}=", ERB.new(value).result())
    end
    ...
  end
end

Based on fixtures files I created this code:

Feature:

Given the following "Inquirers":
  | id | email                    | start_date        |
  |  1 | [email protected] | <%= Time.now %>   |

Helper:

Given(/^the following "(.*?)":$/) do |model, table|
  table.hashes.each do |hash|
    attributes = Rack::Utils.parse_nested_query(hash.to_query)
    object     = model_name.classify.constantize.new

    attributes.keys.each do |key|
      object.send("#{key}=", ERB.new(value).result())
    end
    ...
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文