Cucumber 表中的动态数据
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于该表正在由您的步骤定义进行处理,因此您可以在表中放置一个特殊的占位符,例如字符串“TODAYS_DATE”,然后使用
map_column!
处理该列中的数据以你想要的格式。例如,给出下表,
在您的步骤定义中,您将
注意到,这仅在您要求哈希时更改值。 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
In your step definition you would have
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!
我知道自从提出这个问题以来已经有很长时间了,但我最近正在做一些与 Cucumber 类似的事情,所以如果有人感兴趣的话,这里有一个替代解决方案...
然后在您的步骤定义中只需 eval() 日期字符串
虽然与 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...
And then in your step definition just eval() the date string
Though unlike Brandon's example this wont let you put in exact dates as well without some further logic.
如果您想做的话,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.
基于固定装置文件,我创建了以下代码:
功能:
帮助程序:
Based on fixtures files I created this code:
Feature:
Helper: