mocha icon indicating copy to clipboard operation
mocha copied to clipboard

Response body from file content in `testdata/` directory

Open zx8 opened this issue 2 years ago • 0 comments

A pattern I currently follow for all my Go tests is to store JSON response bodies in e.g testdata/all_users.json or testdata/europe_users.json and so on.

The testdata directory is considered the idiomatic way to do this in Go.

It would be super convenient if I could do something like the following, with an error/failure if the file does not exist:

m := mocha.New(t).CloseOnCleanup(t)
m.Start()
m.AddMocks(mocha.
    Get(expect.URLPath("/users")).
    Reply(reply.OK().BodyFile("testdata/all_users.json)))

Currently I'm using a separate helper function to read the files and pass the string in using BodyString(...)

zx8 avatar Sep 04 '23 22:09 zx8