我有一些控制器测试,我想检查响应值。 我无法将JSON.parse(...["start_time"])与foo.start_time与以下堆栈跟踪进行比较:
expected: 2016-09-17 10:50:00.000000000 +0000 got: "2016-09-17T10:50:00.000Z"我尝试了一堆解析技术,但它们要么返回不同的格式,要么不返回准确的值。 有没有一种简单的方法可以将这些值用于相同的格式以比较相等性?
I have some controller tests and I want to check the response values. I am unable to compare JSON.parse(...["start_time"]) with foo.start_time with the following stack trace:
expected: 2016-09-17 10:50:00.000000000 +0000 got: "2016-09-17T10:50:00.000Z"I tried a bunch of parsing techniques but they either return different formats, or do not return accurate values. Is there a simple way to get these values in to the same format to compare equality?
最满意答案
您正在尝试比较两种不同的数据类型。 解析的JSON是一个字符串。 foo.start_time属性是Ruby日期时间。
请执行下列操作:
(JSON.parse(...["start_time"])).to_datetime这会将字符串转换为日期时间,然后可以将其与foo.start_time进行比较。
You're trying to compare two different data types. The parsed JSON is a string. The foo.start_time attribute is a Ruby datetime.
Do the following:
(JSON.parse(...["start_time"])).to_datetimeThat will convert the string into a datetime, which can then be compared to foo.start_time.
更多推荐
发布评论