jsonassert icon indicating copy to clipboard operation
jsonassert copied to clipboard

[Feature Request] Generate expected output

Open sazzer opened this issue 3 years ago • 1 comments

What problem are you trying to solve?

I understand that this is an assertion tool and not a snapshotting tool, but it's the best choice for asserting that the JSON returned from an API call is correct, and in general it works fantastically for this.

However, it is slightly clunky when you are first writing a test. Unless you know exactly what the JSON should look like, you either need to generate it manually and paste into the test or else go through one value at a time fixing up errors until the test passes. Both of which are awkward.

(And yes, I'm aware this is slightly lazy, but it's super convenient especially when there's a lot of large JSON responses to check)

Describe how you wish this package would help you solve the problem

It would be fantastic if there were some way that the library could output the full actual JSON in some cases. Maybe if the expected JSON were just the blank string, for example.

That way you could write a test of

	ja.Assertf(string(body), ``)

Run the test, copy the output JSON into the test and know that it's correct.

sazzer avatar Sep 15 '22 16:09 sazzer

@sazzer Thanks for submitting this issue.

This package will print the whole string being given if it's not valid JSON. So if you pass in something that's ALMOST correct JSON (e.g. ">"+string(body)), then you should see the full JSON printed.

That said, it's not being pretty printed (because it can't pretty print something that it believes isn't JSON), so this works best for small payloads.

If you want a pretty printed string you can write something like:

var pretty bytes.Buffer
_ = json.Indent(&pretty, body, "", "\t")
t.Log(string(pretty.Bytes())) // will only print if test fails or -v flag is used

Let me know if this doesn't sufficiently solve your problem.

I'm not against implementing a Golden File-like solution -- I actually think that's a good idea -- but I don't think just printing the actual payload given a specific expected payload is the right developer experience for solving the core problem.

kinbiko avatar Sep 16 '22 05:09 kinbiko