Quantcast
Channel: User Schwern - Stack Overflow
Viewing all articles
Browse latest Browse all 581

Answer by Schwern for ISO 8061 built-in support in RSpec or Minitest

$
0
0

If the purpose of the test is to check that the pin_code_sent_at pair has been converted to JSON correctly, you can make this easier with have_attributes. You can test individual keys without having to change the test every time a key is added.

expect(parsed_body).to have_attributes("pin_code_sent_at": user.pin_code_sent_at.utc.iso8601(3))

You can also simply round-trip your expected data through JSON and see if they match. It's called "round tripping", ensuring the data can be serialized and deserialized correctly. This is fine, you're not testing the JSON parser, you're testing it was passed the correct data. It avoids false test failures when the details of the JSON formatting change.

expect(parsed_body).to have_attributes(  JSON.parse({ pin_code_sent_at: user.pin_code_sent_at }.to_json))

If you find yourself doing this a lot, you can write a custom matcher.

RSpec::Matchers.define :have_json_attributes do |expected|  match do |actual|    expect(actual).to have_attributes(JSON.parse(expected.to_json))  endendexpect(parsed_body).to have_json_attributes(  pin_code_sent_at: user.pin_code_sent_at)

(Something like that, I can't test it at the moment.)


Viewing all articles
Browse latest Browse all 581

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>