Unit Test with Mongoose

前端 未结 4 1811
渐次进展
渐次进展 2021-01-01 17:21

I\'m new to Node.js, Mongoose, and testing in this environment. I have the following schema declared in a separate file.

Issue = mongoose.model(\"Issue\", {         


        
4条回答
  •  感动是毒
    2021-01-01 18:24

    Using mocha with chaijs and sinonjs in my node code something like this method works for me:

    var should = require('chai').should(),
    sinon = require('sinon'),
    mongoose = require('mongoose');
    
    it('#issues() handles mongoosejs errors with 403 response code and a JSON error message', function (done) {
    
    // mock request
    var request = {};
    
    // mock response
    var response = {};
    response.setHeader = function(header) {};
    response.send = function (responseCode, jsonObject) {
        responseCode.should.equal(403);
        jsonObject.stats.should.equal('error');
        // add a test for "error:": exception
        done();
    }
    
    var mockFind = {
        sort: function(sortOrder) {
            return this;
        },
        exec: function (callback) {
            callback('Error');
        }
    }
    
    // stub the mongoose find() and return mock find 
    mongoose.Model.find = sinon.stub().returns(mockFind);
    
    // run function
    issues(request, response);
    
    });
    

提交回复
热议问题