Stephani Knupp
Stephani Knupp

Reputation: 77

How to expect for JSON in tests

The function I'm testing is returning JSON. How can I write the exact JSON object I expect in the want field?

tests := []struct {
        name string
        args args
        want string
    }{
        {
            name: "success",
            args: args{ "XXX", "XXX" },
            want: "XXX",
        },

Upvotes: 1

Views: 50

Answers (1)

obviyus
obviyus

Reputation: 144

Your example is sufficient if you enclose the JSON into a block, i.e.:

var tests = []struct {
    name string
    args args
    want string
}{
    {
        name: "success",
        args: args{ "XXX", "XXX" },
        want: `
                {
                    "example": [
                        {
                            "name": "example",
                            "inputs": [],
                            "outputs": [],
                        }
                    ]
                }
        `,
    },
}

Upvotes: 1

Related Questions