I’m new to jasmine, and I want to write some tests for mongodb operations.
Suppose I have several .spec.js files under directory spec, and all the tests within them should drop the testing database and inserting some testing data.
How should I organize the code? Shall I define an init() method somewhere, and call it from all other tests? Or is there any better solution?
Inside your
describeyou can callbeforeEachandafterEachas usual with test frameworks. This is the right place to do setting up and tearing down of test prerequisites.For example:
In any case though best practice would be to not actually do the db operations: Presumably you use some library that is already tested. It is going to save you a lot of hassle if you mock your db layer. You can do this effectively with jasmine by using spies.
For example let’s assume your db layer has a
get(id)function that returns some json. You could be doing: