lundi 3 septembre 2018

Approaches to testing negative scenarios in Go

I'm wondering if there's an accepted/idiomatic way of testing an unexpected behavior when working with external systems (such as databases). These are mostly cases within "if err != nil {...}", when normally the error just doesn't happen and you don't control it through the inputs.

One way of doing that cleanly is probably defining an interface and a mock structure that would return error when you need it. But if I already have a significant amount of code that doesn't work with interfaces, bringing them just for the sake of testing a couple of scenarios seems tiresome.

So does anyone know and use different approaches? For example, in dynamic languages such as php and js a function/method behavior can be easily overridden with a mocking library or even manually, which is quite useful when writing tests.

Aucun commentaire:

Enregistrer un commentaire