【问题标题】:Rspec Array match partial string timestampRspec Array 匹配部分字符串时间戳
【发布时间】:2016-08-30 13:49:23
【问题描述】:

我有一个这样的数组:

["xxxxxxxxx", 448, "2016-08-29", 1, "Trading", 53, "Trading", 2016-06-03 00:00:00 -0500, 2016-06-03 00:00:00 -0500, "RBS", "USD", "United States Dollar", nil, 2016-10-08 00:00:00 -0500, 2016-09-08 00:00:00 -0500, "2016-09-08 00:00:00 -0500","lcz076", nil, 5, 2016-10-08 00:00:00 -0500, 2016-09-08 00:00:00 -0500, "9", 448, "P", nil]

我只需要为这个值匹配一个值"2016-09-08 00:00:00"

我该怎么做?

【问题讨论】:

  • 你只是想检查它是否在里面吗?然后使用.include?

标签: ruby selenium rspec assert


【解决方案1】:

首先将所有日期转换为Strings

然后将其设置为变量a

a = ["xxxxxxxxx", 448, "2016-08-29", 1, "Trading", 53, "Trading", "2016-06-03 00:00:00 -0500, 2016-06-03 00:00:00 -0500"...

我注意到您要检查的所有日期类型都有-0500,因此您可以通过在数组中循环来取出该部分

new_str[i] = str.slice(0..(str.index('-0500')))

现在你可以使用.include?("stuff here")

【讨论】:

【解决方案2】:

假设你有:

a = ["xxxxxxxxx", 448, "2016-08-29", 1, "Trading", 53, "Trading", "2016-06-03 00:00:00 -0500", "2016-06-03 00:00:00 -0500", "RBS", "USD", "United States Dollar", nil, "2016-10-08 00:00:00 -0500", "2016-09-08 00:00:00 -0500", "2016-09-08 00:00:00 -0500", "lcz076", nil, 5, "2016-10-08 00:00:00 -0500", "2016-09-08 00:00:00 -0500", "9", 448, "P", nil] 

RSpec 期望应该是:

matches = a.select{ |x| x.to_s.match("2016-09-08 00:00:00") }
expect(matches).not_to be_empty

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 2019-10-18
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多