dimanche 20 décembre 2015

How to use multiple assertions in a single test

Note: Similar questions - have been asked, and addressed, but not in the context of Mocha.

Sometimes it's useful to have multiple assertions to validate the functionality of a single function / action. A simple example would be an email address validator where the test should consider multiple patterns (see this answer).

In Mocha, we have describe(desc, function(){...}) and in it we have it(desc, function(){...}).

It's possible to have many it(...) calls inside a describe(...) for the many assertions, but that seems bloated with boilerplate code and overly verbose, particularly considering the generated output (That would create a long list of "passed" test where all that's really interesting is that "IsValidEmail" works).

Alternatively, it's possible to use many assert / expect / should etc... within a single it(...) but then tests are run till the first failure (other tests keep reporting as usual). While that's generally ok, it's more useful to have a list of all the failed patterns, again, considering the email validation example, it's useful to know that neither "@" is required and that the top-level domain is a invalid in the same test run.

Solutions to this problem exist in other domains (as can be seen in the links above), but I've found no useful way of doing this with Mocha.

How can this be done efficiently?

Aucun commentaire:

Enregistrer un commentaire