cucumber - Cucumber 表中的动态数据

标签 cucumber bdd

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

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

最佳答案

由于您的步骤定义正在处理该表,因此您可以在表中放置一个特殊的占位符,例如字符串“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 中的代码进行转换!

关于cucumber - Cucumber 表中的动态数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1617937/

相关文章:

javascript - 如何为包含 HTML 和 JavaScript 或 jQuery 的网页编写 Jasmine 测试?

ruby - 在 Cucumber 中限定转换范围

android - 集成测试和 Cucumber 测试

java - eclipseoxy中的 cucumber eclipse插件

ruby - 如何使用 RSpec 模拟/ stub 文件目录及其内容?

c# - 如何在 Specflow 中管理全局变量

android - Cucumber 测试未按指定顺序运行,出了什么问题?

java - 对象未使用 PageFactory.initElements(driver,class); 进行初始化

ruby-on-rails - RSpec 错误 : test couldn't find table

bdd - 启动 BDD 资源?