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 = require('test-prepare')({
mongo_host: 'http://localhost:27017',
fixtures_path: '/path-to-fixtures',
verbose: true
});
- 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.
Ex: ${__dirname}/../fixtures
- verbose: Enable verbose mode. This will log test prepare info and feedbacks about connection and fixtures imports. Default is false.
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
Example, people.json:
{
"model": "People",
"fixtures": [
{
"name": "Jhon",
"age": 31
},
{
"name": "Michael",
"age": 25
}
]
}
before(function (done) {
prepare.start(['fixture_file_name_without_extension', 'other_fixture_file_name_without_extension', '...']).then(done);
});
So if you want to import your fixture file people.json:
before(function (done) {
prepare.start(['people']).then(done);
});
And importing many fixtures:
before(function (done) {
prepare.start(['people', 'cars', 'telephones']).then(done);
});
After test you can clean test data ro reset your test scenario. This uill drop test-prepare temporary database.
after(function (done) {
prepare.end().then(done);
});
If you want change fixture data before import to the database, you can use the middleware function.
prepare._importFixture('people', middleware, callback);
prepare._importFixture('people', function(data) {
// change fixture data through middleware then import to the database.
data.fixtures[0].name = 'Jhon Doe';
return data;
})
.then(() => {
//.... here on callback, you make your test asserts ...
var result = example_get_by_name('Jhon Doe');
expect(result.length).to.be.equal(1);
done();
});
Whenever a fixture is imported, its data is accessible through the fixture_fixtureName property that is exposed in the test-prepare object.
beforeEach(function (done) {
prepare.start(['people', 'cars']).then(done);
});
it('my test', function(done) {
var people = prepare.fixture_people;
var cars = prepare.fixture_cars;
done();
});