简体   繁体   English

rspec期望的日期等于

[英]rspec expect date to equal

I have an object called a work order that has the following schema: 我有一个称为工作订单的对象,该对象具有以下架构:

# == Schema Information
#
# Table name: work_orders
#
#  id                 :integer          not null, primary key
#  due_on             :date
#  name               :string(255)

I also have a Json output of the object when it is displayed: 显示该对象时,我还有一个Json输出:

{
   id: 1,
   due_on: "2015-06-08",
   name: "my work order"
}

I am attempting to use rspec to confirm that date displayed by the json, matches the date of the object. 我试图使用rspec确认json显示的日期与对象的日期匹配。 This is something I have done 50 times for other objects... 这是我为其他对象所做的50次...

I have the following: 我有以下几点:

exemplar = FactoryGirl.create(:work_order)
...
puts "Test: #{exemplar.due_on}"
expect(exemplar.name).to      eq(hash['name'])   #This works fine!
expect(exemplar.due_on).to    eq(hash['due_on']) #This blows up...

The output I get is: 我得到的输出是:

Test: 2015-06-08

expected: "2015-06-08"
     got: Mon, 08 Jun 2015

What I do not understand is where the "Mon, 08 Jun 2015" is coming from. 我不明白“ 2015年6月8日星期一”的来历。 when I print out the date with the puts statement, it shows it as 2015-06-08... 当我用puts语句打印日期时,它显示为2015-06-08 ...

Rails converts the string to a date object. Rails将字符串转换为日期对象。 2015-06-08 is the output from Date#to_s : 2015-06-08Date#to_s的输出:

d = Date.today
#=> Thu, 17 Jul 2014

puts d
#=> 2014-07-17

You can compare the date object to your string with either: 您可以使用以下任一方法将date对象与您的字符串进行比较:

expect(exemplar.due_on.to_s).to eq(hash['due_on'])

or: 要么:

expect(exemplar.due_on).to eq(Date.parse(hash['due_on']))

or using Active Support Core Extensions : 或使用Active Support Core Extensions

expect(exemplar.due_on).to eq(hash['due_on'].to_date)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM