thoughtbot / json_matchers

Validate your JSON APIs
MIT License
384 stars 40 forks source link

Make test data consistent via factories #73

Closed seanpdoyle closed 6 years ago

seanpdoyle commented 6 years ago

Introduce object traits

A Schema created by a factory call with the object trait will declare an object that has { "id": { "type": "number" } }.

A Response created by a factory call with the object trait will create an instance that serializes to an array with { "id": 1 }. A corresponding invalid_object trait will build a response that has an "id" key that is not a number.

Additionally, this commit extracts the FakeResponse and FakeSchema classes out of the factory file and into files of their own.

Instances of both classes now respond to #to_h so that they can be passed directly to raise_error_containing, without any coercion at the call site.

Instances of FakeSchema now respond to #to_s so that they can be passed directly to match_json_schema.

ActiveSupport is added as a dependency, so that raise_error_containing can invoke String#squish to normalize the serialized exceptions and the messages passed into the expectation.

Group failure message tests

Restructure some test blocks to group together tests that assert the shape and contents of assertive and negated failure messages.

When exercise-level variables are coerced into Strings, Hashes, and Arrays, separate variables into the factory-built variables, and more intention-revealing coerced variables.