pact-ruby: Integers from the provider fail validation against a decimal expectation

Consider the following response from a provider:

"ratings": [
  {
    "id": 12,
    "count": 4,
    "average": 3.75
  },
  {
    "id": 37,
    "count": 1,
    "average": 4
  },
  {
    "id": 143,
    "count": 0,
    "average": 0
  },
  {
    "id": 574,
    "count": 6,
    "average": 4.333333
  },
]

The response body in the interaction is defined as follows (Javascript):

ratings: eachLike({
  id: 15,
  count: 20,
  average: 4.5
})

Validating the provider yields the following errors:

Expected a Float (like 4.5) but got a Fixnum (4)...
Expected a Float (like 4.5) but got a Fixnum (0)...

Please allow Fixnums in the provider response to validate successfully against Float expectations.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 20 (13 by maintainers)

Commits related to this issue

Most upvoted comments

Ok, I’ve put out version 1.67.0 of the ruby standalone. I was tempted to make a float match an int, but an int not match a float as a kind of backwards compatible intermediate step, but then I thought about the whole (1.0 == 1) == true thing, and decided it wasn’t worth it.

I haven’t exposed a flag to use the old matching logic, but I can if anyone asks for it.

Just did a quick spike locally, and modifying this file with this extra condition as per below resolves the issue:

|| (expected.is_a?(Float) && actual.is_a?(Fixnum))

As Beth mentioned, however, this would result in a backward-incompatible change so it would at best need to be wrapped behind a feature flag. I’d defer to Beth here on the best path forward, but I do hear your pain.

I upgraded to latest JS version (8.2.6) and that in turn upgraded Pact Node to 8.6.0 which includes the Ruby fix by @bethesque. My assertions now work!!!

Thank you so very much for this change! It puts our contract testing effort back on track.

Thank you! Thank you! Thank you!

For posterity, the updated release that went out was 1.68.0 of the standalone. Version 8.6.0 of Pact Node going out, and I’ll update Pact JS to use the latter dependency soon.