使用嘲讽和sinon模拟类方法 [英] Mock a class method using mockery and sinon

查看:105
本文介绍了使用嘲讽和sinon模拟类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习使用带有sinon的节点模块模拟进行单元测试.

I'm learning to unit test using the node module mockery with sinon.

仅使用嘲笑和普通类,就可以成功注入模拟.但是,我想注入一个sinon存根而不是普通的类,但是我对此有很多麻烦.

Using only mockery and a plain class I'm able to inject a mock successfully. However I would like to inject a sinon stub instead of a plain class but I'm having a lot of troubles with this.

我要模拟的课程:

function LdapAuth(options) {}

// The function that I want to mock.
LdapAuth.prototype.authenticate = function (username, password, callback) {}

这是我的beforeEach()函数当前正在使用的代码:

And here is the code I'm currently using in my beforeEach() function:

    beforeEach(function() {
        ldapAuthMock = sinon.stub(LdapAuth.prototype, "authenticate", function(username, password, callback) {});
        mockery.registerMock('ldapauth-fork', ldapAuthMock);
        mockery.enable();
    });

    afterEach(function () {
        ldapAuthMock.restore();
        mockery.disable();
    });

我试图以各种方式模拟/存根LdapAuth类,但没有成功,并且上面的代码只是不起作用的最新版本.

I've tried to mock/stub the LdapAuth class in various ways without success and the code above is just the latest version that doesn't work.

所以我只想知道如何使用sinon和嘲讽来成功地对此进行模拟.

So I just want to know how to mock this successfully using sinon and mockery.

推荐答案

由于Node的模块缓存,Javascript的动态特性以及它的原型继承性,这些节点模拟库可能非常麻烦.

These node mocking libraries can be quite cumbersome because of Node's module cache, Javascript's dynamic nature and it's prototypical inheritance.

幸运的是,Sinn还负责修改您要模拟的对象,并提供了一个不错的API来构造间谍,潜艇和模拟物.

Fortunately Sinon also takes care of modifying the object you are trying to mock as well as providing a nice API to construct spys, subs and mocks.

这是我如何对authenticate方法进行存根的小例子:

Here is a small example of how I would stub the authenticate method:

// ldap-auth.js

function LdapAuth(options) {
}

LdapAuth.prototype.authenticate = function (username, password, callback) {
  callback(null, 'original');
}

module.exports = LdapAuth;

// test.js

var sinon = require('sinon');
var assert = require('assert');
var LdapAuth = require('./ldap-auth');

describe('LdapAuth#authenticate(..)', function () {
  beforeEach(function() {
    this.authenticateStub = sinon
                              // Replace the authenticate function
                              .stub(LdapAuth.prototype, 'authenticate')
                              // Make it invoke the callback with (null, 'stub')
                              .yields(null, 'stub');
  });

  it('should invoke the stubbed function', function (done) {
    var ldap = new LdapAuth();
    ldap.authenticate('user', 'pass', function (error, value) {
      assert.ifError(error);
      // Make sure the "returned" value is from our stub function
      assert.equal(value, 'stub');
      // Call done because we are testing an asynchronous function
      done();
    });
    // You can also use some of Sinon's functions to verify that the stub was in fact invoked
    assert(this.authenticateStub.calledWith('user', 'pass'));
  });

  afterEach(function () {
    this.authenticateStub.restore();
  });
});

我希望这会有所帮助.

这篇关于使用嘲讽和sinon模拟类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆