Testing a DateTime field from a Rails API with RSpec

Posted by ZedTuX 0n R00t on March 7, 2018

You are testing a simple API returning object’s attributes including the created_at and the updated_at fields:

You write a simple RSpec test:

But you got an issue comparing the dates:

      -  "created_at"=>"2018-03-07 14:22:36.199832700 +0100",
      +  "created_at"=>"2018-03-07T14:22:36.000+01:00",

So close …

The solution is quite simple but took me few minutes in order to find it. It’s all about using the iso8601 method in your Rspec files and you can pass the length of fractional seconds in parameter.