【问题标题】:Rspec fails because date is not in correct formatRspec 失败,因为日期格式不正确
【发布时间】:2018-08-11 03:35:19
【问题描述】:

我在 Rspec 中有一个这样的例子:

describe "#parse" do
  context "when disposition date is present" do
    it "parses data" do
        expect(@practice.parse[0]).to match_array [ nil, "502011TR053942AXXXNB", Date.parse("2011-02-23") ]
    end
  end
end  

生成到数组中的日期是这样生成的:

Date.strptime(date.strftime("%m/%d/%Y"), "%m/%d/%Y")                                                   

所以它是一个 Date 对象。

但是,Rspec 失败了:

失败/错误: 期望(@practice.parse[0]).to match_array [ 无,“502011TR053942AXXXNB”,Date.parse(“2011-02-23”)]

包含的预期集合:[nil, "502011TR053942AXXXNB", Wed, 23 2011 年 2 月]

它说它预计“2011 年 2 月 23 日,星期三”。但这不只是在 Date 对象上调用的 to_s 吗?我该如何处理?

【问题讨论】:

    标签: ruby rspec


    【解决方案1】:

    写下来:

    expect(@practice.parse[0]).to match_array [ nil, "502011TR053942AXXXNB", Date.parse("2011-02-23").strftime("%a, %d %b %Y") ]
    

    here读取指令。

    ~$ ruby -e 'require "date";puts Date.parse("2011-02-23").strftime("%a, %d %b %Y")'
    # => Wed, 23 Feb 2011
    

    【讨论】:

    • 实际上,当我再次查看 Rspec 输出时,我注意到它确实匹配正确:预期集合包含:[nil, "502011TR053942AXXXNB", Wed, 2011 年 2 月 23 日。该问题与这个。问题是原始数组在其他地方使用 Time.now。
    猜你喜欢
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    相关资源
    最近更新 更多