项目作者: supasate

项目描述 :
A promise fixture loader for Mongoose
高级语言: JavaScript
项目地址: git://github.com/supasate/mongoose-fixture-loader.git
创建时间: 2016-08-01T19:08:04Z
项目社区:https://github.com/supasate/mongoose-fixture-loader

开源协议:Apache License 2.0

下载


Mongoose Fixture Loader

Build Status

A promise fixture loader for Mongoose.

  1. Load a single object

    1. loadFixture(UserModel, userObject);
  2. Load an array of objects

    1. loadFixture(UserModel, arrayOfUserObjects);
  3. Load a sequence of objects

    1. loadFixture(UserModel, userObject)
    2. .then((userInstance) => {
    3. loadFixture(BookModel, bookObjectRelatedToUserObject)
    4. });
  4. Load objects in parallel

    1. Promise.all([
    2. loadFixture(UserModel, userObject),
    3. loadFixture(CatModel, catObject),
    4. loadFixture(DogModel, dogObject)
    5. ]);

Installation

npm install --save mongoose-fixture-loader

Usage

  1. Assume you have a user model file src/models/user-model.js as the following.

    1. const mongoose = require('mongoose');
    2. const UserSchema = new mongoose.Schema({
    3. firstName: { type: String, required: true },
    4. lastName: {type: String, required: true },
    5. email: { type: String },
    6. created: { type: Date, default: Date.now }
    7. });
    8. module.exports = mongoose.model('User', UserSchema);
  2. Create a fixture file test/fixtures/user.js to export a JSON object,

    1. module.exports = {
    2. firstName: 'John',
    3. lastName: 'Doe',
    4. email: 'john.doe@test.com'
    5. };

    or an array of JSON object.

    1. module.exports = [
    2. {
    3. firstName: 'John',
    4. lastName: 'Doe',
    5. email: 'john.doe@test.com'
    6. },
    7. {
    8. firstName: 'Alice',
    9. lastName: 'Bob',
    10. email: 'alice.bob@test.com'
    11. }
    12. ];
  3. In your test file test/index-test.js, you can load and test your fixture.

    1. const expect = require('chai').expect;
    2. const mongoose = require('mongoose');
    3. const loadFixture = require('mongoose-fixture-loader');
    4. const UserModel = require('../src/models/user-model.js');
    5. const user = require('./fixtures/user.js');
    6. // Mongoose default promise is deprecated
    7. mongoose.Promise = global.Promise;
    8. describe('a test suite', () => {
    9. mongoose.connect('mongodb://localhost/mongoose-fixture-loader-test');
    10. before((done) => {
    11. loadFixture(UserModel, user)
    12. .then((userInst) => {
    13. done();
    14. })
    15. .catch((err) => {
    16. done(err);
    17. });
    18. });
    19. after((done) => {
    20. UserModel.remove({})
    21. .then(() => {
    22. return mongoose.connection.close();
    23. })
    24. .then(() => {
    25. done();
    26. })
    27. .catch((err) => {
    28. done(err);
    29. });
    30. });
    31. it('should find John', (done) => {
    32. UserModel.find({})
    33. .then((users) => {
    34. expect(users[0].firstName).to.equal('John');
    35. done();
    36. })
    37. .catch((err) => {
    38. done(err);
    39. });
    40. });
    41. });
  4. Enjoy testing!

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

Copyright (c) 2016 Supasate Choochaisri

Licensed under the Apache License.