在Node.js中模拟模块以进行单元测试

19

我想对一个node.js模块中的一些函数进行单元测试。 我认为模拟第三个模块会很有帮助,特别是避免访问数据库。

# models/account.coffee
register = (email, password)->
   sha_sum.update(password)
   pw = sha_sum.digest('hex')
   user = 
      email: email
      password: sha_sum.digest('hex')

   users_db.save user, (err, doc)->
      register_callback(err)

account_module = 
   register: register

module.exports = account_module

这是我想要测试的模块

# routes/auth.coffee
account = require '../models/account'

exports.auth = 
   post_signup: (req, res)->
      email = req.body.email
      password = req.body.password
      if email and password
          account.register(email, password)
          res.send 200
      else
          res.send 400

我希望能够测试在正确的post body下访问此url是否调用了account.register函数,但我不想进行与数据库的交互测试。也许我还没有实现账户模块。

jasmine规范 # specs/auth.test.coffee describe 'signup', ->

   request = require 'request' 
   it 'should signup a user with username and password', (done)->

       spyOn(account, 'register') # this does not work, account.register still called
       url = root + '/signup'
       headers =
           "Content-Type": "application/json" 
       data = 
           email: 'user@email.com'
           password: 'pw'
       body = JSON.stringify(data)
       request {url: url, method: 'POST',json: data, headers: headers }, (err, response, body)->

           expect(response.statusCode).toEqual(200)
           done()

我已经研究了几个用于node.js的模拟库(https://github.com/easternbloc/Syringe, https://github.com/felixge/node-sandboxed-module),但是迄今为止没有成功。无论我在规范(spec)中尝试什么,account.register总是会被执行。这种方法是否有缺陷?


看起来 horaa(https://github.com/arunoda/horaa)可能会起作用。 - Arne Jenssen
尝试使用 sinon.js,这是一个出色的存根/间谍/模拟/模拟服务器库。 - Stephan Kristyn
4个回答

20

我正在使用mocha作为测试框架,并使用sinon进行mock、stub和spy。我建议你将账户模块委托给auth.coffee模块,并像这样进行mock:

exports.init = function (account) {
    // set account object
}

因此,从mocha测试中,您可以创建一个虚拟的账户对象,并在实际测试中使用sinon进行模拟。

describe('some tests', function () {

    var account, response, testObject;

    beforeEach(function () {

        account = {
             register: function () { }
        };

        response = {
            send: function () { }
        };

        testObject = require('./auth');
        testObject.init(account);
    });

    it('should test something', function () {

        var req = { body: { email: ..., password: .... } }, // the request to test
            resMock = sinon.mock(response),
            registerStub = sinon.stub(account, 'register');

        // the request expectations
        resMock.expect('send').once().withArgs(200);

        // the stub for the register method to have some process
        registerStub.once().withArgs('someargs');

        testObject.auth(req. response);

        resMock.verify();

    });

});

很抱歉我没有用coffescript写下来,因为我不习惯使用它。


谢谢。这个方法可行。与劫持模块不同,这种方法使用依赖注入和模拟。 - Arne Jenssen
1
你不需要在 beforeEach 方法中实现 account.register。只需像我一样使用存根并调用 yields 来进行异步回调。查看 sinon 文档以获取有关存根和 yields 的信息。 - Stefan

1
我建议使用proxyquire。它可以实现你想要的功能,而不依赖于依赖注入,这对你的代码是有干扰的,如果你没有按照这种方式编写模块,则需要更改代码。

谢谢,这正是我想要的! - Hosar

0

我一直在使用gently进行模拟和存根,mocha作为测试框架,并使用should.js进行BDD风格的测试。以下是一个示例单元测试:

describe('#Store() ', function () {
    it('will delegate the store to the CacheItem and CacheKey', function () {
        var actualCacheKey, actualConnMgr, actualConfig, actualLogger, actualRequest;
        var actualKeyRequest, actualKeyConfig;

        gently.expect(
            CacheKey, 'CreateInstance', function (apiRequest, config) {
                actualKeyRequest = apiRequest;
                actualKeyConfig = config;

                return mockCacheKey;
            });

        gently.expect(
            CacheItem, 'CreateInstance', function (cacheKey, connectionManager, config, logger, apiRequest) {
                actualCacheKey = cacheKey;
                actualConnMgr = connectionManager;
                actualConfig = config;
                actualLogger = logger;
                actualRequest = apiRequest;

                return mockCacheItem;
            });

        var actualApiRequest, actualCallback;
        gently.expect(mockCacheItem, 'Store', function (request, callback) {
            actualApiRequest = request;
            actualCallback = callback;
        });

        var callback = function () {};
        var apiResponse = {'item': 'this is a sample response from SAS'};
        Cache.GetInstance(connMgr, config, logger).Store(apiRequest, apiResponse, callback);

        mockCacheKey.should.be.equal(actualCacheKey, 'The cachkeKey to CacheItem.CreateIntsance() did not match');
        connMgr.should.be.equal(
            actualConnMgr, 'The connection manager to CacheItem.CreateInstance() did not match');
        config.should.be.equal(actualConfig, 'The config to CacheItem.CreateInstance() did not match');
        logger.should.be.equal(actualLogger, 'The logger to CacheItem.Createinstance did not match');
        apiRequest.should.be.equal(actualRequest, 'The request to CacheItem.Createinstance() did not match');

        apiRequest.should.be.equal(actualKeyRequest, 'The request to CacheKey.CreateInstance() did not match');
        config.should.be.equal(actualKeyConfig, 'The config to CacheKey.CreateInstance() did not match');

        callback.should.be.equal(actualCallback, 'The callback passed to CacheItem.Store() did not match');
        apiResponse.should.be.equal(actualApiRequest, 'The apiRequest passed to CacheItem.Store() did not match');
    });
});

0

Stefan的解决方案可行。我只是添加了一些细节。

    describe 'register', ->
    account = response = routes_auth = null

    beforeEach ->
        account =
            register: (email, pw, callback)-> 
                if email is 'valid@email.com'
                    callback(null, 1)
                else
                    err = 'error'
                    callback(err, 0)

        response = 
            send: -> {}

        routes_auth = require('../routes/auth').init(account)


    it 'should register a user with email and pw', (done)->
        req =
            body:
                email: 'valid@email.com'
                password: 'pw'

        resMock = sinon.mock(response)
        resMock.expects('send').once().withArgs(200)
        routes_auth.post_register(req, response)
        resMock.verify() 
        done()



    it 'should not register a user without email', ()->
        req =
            body:             
                password: 'pw'

        resMock = sinon.mock(response)
        resMock.expects('send').once().withArgs(400)
        routes_auth.post_register(req, response)
        resMock.verify() 

routes/auth.coffee 模块 ...

exports.init = (account)->
    get_available: (req, res)->
        email = req.param.email
        if not email? or email.length < 1
            res.send 400
            return
        account.available email, (err, doc)->
            console.log 'get_available', err, doc
            if err then res.send 401
            else res.send 200


    post_register: (req, res)->
        email = req.body.email
        password = req.body.password
        if email and password
            account.register email, password, (err, doc)->
                if err then res.send 401
                else res.send 200
        else
            res.send 400

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接