jeudi 22 octobre 2015

How do I write a Mongoose.js model test decoupling the create and find function?

I'm testing a Mongoose.js model and want to test if the model is being created and read properly independent of each other. Is it possible to test reading a document without having to create it? It seems that they are not mutually independent and much be tested together.

'use strict';

var app = require('../../server.js'),
    request = require('supertest'),
    should = require('should'),
    async = require('async'),
    mongoose = require('mongoose'),
    Venue = mongoose.model('Venue');

describe('Venues model test', function() {
    describe('venues route should be wired up', function() {
        it.skip('should respond with 200', function(done) {
            request(app)
                .get('/api/venues')
                .expect(200)
                .end(function(err, result) {
                    done();
                });
        });
    });

    describe('venue\'s basic CRUD functions', function() {
        // create / read / update / delete

        //CREATE + FIND ??
        describe('Create a venue document', function() {
            // remove all the documents
            before(function(done) {
                Venue.remove(function() {
                    done();
                })
            });
            // create and save a document
            it('should create a venue document and find it', function(done) {
                async.waterfall([
                    function (callback) {
                        var venue = new Venue({name: "Venue name"});
                        venue.save(function(err) {
                            if (err) callback(err);
                            callback();
                        });
                    },
                    function(callback) {
                        // Find the saved document and check against the newly created document
                        Venue.find({name: 'Venue name'}, function(err, venueDocument) {
                            if (err) callback(err);
                            venueDocument.should.be.an.instanceOf(Array).and.have.lengthOf(1);
                            venueDocument[0].should.have.property('name', 'Venue name');
                            callback();
                        });
                    }
                ], function (err) {
                    if (err) callback(err);
                    done();
                })
            });
        });


    });
});

Aucun commentaire:

Enregistrer un commentaire