waterlink / rspec-json_expectations

Set of matchers and helpers to allow you test your APIs responses like a pro.
https://www.relishapp.com/waterlink/rspec-json-expectations/docs
MIT License
140 stars 23 forks source link

DateTime is not considered a supported value #29

Open esjee opened 6 years ago

esjee commented 6 years ago

Hey! I recently ran into the "issue" where doing an expectation like this:

# record = ...
# ^ some active record object 
expected_response = {
  updated_at: record.updated_at
}
expect(response.body).to include_json expected_response

which raises

     NotImplementedError:
       2017-11-13 13:37:00 UTC expectation is not supported

the fix here is to use .as_json, like so:

# record = ...
# ^ some active record object 
expected_response = {
  updated_at: record.updated_at.as_json
}
expect(response.body).to include_json expected_response

For what it's worth, it seems that datetimes are serialized to datetime strings in the Zulu timezone.

I was wondering if you (or others!) would find it useful to automagically detect datetimes like this in the provided expected json, and run as_json on any datetime objects encountered.

waterlink commented 5 years ago

What if we take it further and detect things that have a as_json method defined on them, and try to use that?