Test-Prepare is a MondoDB test preparer. Is designed to clean and import test fixtures before a test to simulate scenarios and go back to initial stage after test.
First install node.js and mongodb. Then
$ npm install test-prepare --save-dev
Import test-prepare in your test file.
var prepare =mongo_host: ''fixtures_path: '/path-to-fixtures'verbose: true;
Supported setup options
- mongo_host: Host address of MongoDB, required
- mongo_user: User of MongoDB, not required
- mongo_password: Password of MongoDB, not required
- fixtures_path: Phisical path of fixtures folder, required.
- verbose: Enable verbose mode. This will log test prepare info and feedbacks about connection and fixtures imports. Default is false.
Create fixture file
Inside fixtures folder, create a fixture file that will be imported to the test database.
- model: Name od MongoDB collection
- fixtures: Array of data that will be imported
Prepare database before test
So if you want to import your fixture file people.json:
And importing many fixtures:
Dropping database after test
After test you can clean test data ro reset your test scenario. This uill drop test-prepare temporary database.
Change fixture data before import
If you want change fixture data before import to the database, you can use the middleware function.
prepare._importFixture('people', middleware, callback);
Accessing fixture data
Whenever a fixture is imported, its data is accessible through the fixture_fixtureName property that is exposed in the test-prepare object.