Mockgoose:如何模拟猫鼬中的错误?

Posted

技术标签:

【中文标题】Mockgoose:如何模拟猫鼬中的错误?【英文标题】:Mockgoose: how to simulate a error in mongoose? 【发布时间】:2016-11-13 11:52:16 【问题描述】:

我正在尝试围绕 mongoose 驱动的应用程序进行单元测试。 虽然 mockgoose 在模拟 mongoose 方面做得很好,所以我可以围绕它进行测试,但我没有找到一种方法让它调用失败,所以我可以测试错误处理逻辑。

它是受支持的用例吗?还是我应该寻找另一个框架?

代码:

var mongoose = require('mongoose');

var Test = ,
    Doc = require('./model/Doc.js');

var dbURL = 'mongodb://127.0.0.1/',
    dbName = 'Test';

function connect(callback) 
    Test = mongoose.createConnection(dbURL + dbName); //<-- Push this to fail
    Test.on('error', (err) => 
        callback(err);
    );
    Test.once('open', function () 
        callback();
    ); 

测试:

var chai = require('chai'),
    expect = chai.expect,
    util = require('util');

var config = require('./config.json');

var proxyquire =  require('proxyquire').noPreserveCache();
var sinon = require('sinon');
var mongoose = require('mongoose');
var mockgoose = require('mockgoose');

describe('test', () => 
    describe('Connect', () => 
        beforeEach((callback) => 
            mockgoose(mongoose).then(() => 
                callback();
            );
        );

        it('Expect to connect to the database', (done) => 
            var stub = 
                    mongoose: mongoose
                ,
                test = proxyquire('./../test.js', 
                    'mongoose': stub.mongoose
                );

            test.connect((err) => 
                try 
                    expect(err).to.not.be.ok;
                    done();
                 catch(err) 
                    done(err);
                
            );

        it('Expect to throw error when failing to connect to the database', (done) => 
            var stub = 
                    mongoose: mongoose
                ,
                medical = proxyquire('./../medical.js', 
                    'mongoose': stub.mongoose
                );

            medical.connect((err) => 
                try 
                    expect(err).to.be.ok;
                    done();
                 catch(err) 
                    done(err);
                
            );
        );
    );
);

结果:

Connect
      ✓ Expect to connect to the database
      1) Expect to throw error when failing to connect to the database


  1 passing (234ms)
  1 failing

  1) Medical Connect Expect to throw error when failing to connect to the database:
     AssertionError: expected undefined to be truthy

【问题讨论】:

【参考方案1】:

我最终存根 mongoose.createConnection(所以生成器)返回一个调用错误的对象。

            let stub = 
                mongoose: 
                    createConnection: () => 
                        return 
                            on: (eventName, callback) => 
                                if(eventName === 'error') 
                                    callback('Medical Error');
                                
                            ,
                            once: () => 
                        
                    
                
            ,
            medical = proxyquire('./../medical.js', 
                'mongoose': stub.mongoose
            );

【讨论】:

以上是关于Mockgoose:如何模拟猫鼬中的错误?的主要内容,如果未能解决你的问题,请参考以下文章

如何模拟猫鼬?

如何在猫鼬中返回自定义错误?

如何在猫鼬中捕获错误代码

猫鼬中的条件运算符

护照本地猫鼬中的密码验证器选项不起作用

如何在猫鼬中转换为字符串?