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

时间:2013-04-03 14:43:05

标签: node.js unit-testing mocking jasmine

我想在node.js模块中对一些函数进行单元测试。我认为嘲笑第3个模块会有所帮助。特别要避免命中数据库

# 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

我希望能够测试在帖子中调用account.register函数时使用正确的正文敲击此URL但我不希望测试命中数据库。我可能还没有实现帐户模块。

茉莉花规格     #specs / auth.test.coffee     描述'注册', - >

   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/Syringehttps://github.com/felixge/node-sandboxed-module)的几个模拟库,但到目前为止还没有成功。无论我在规范中尝试什么,account.register总是被执行。整个方法是否有缺陷?

4 个答案:

答案 0 :(得分:16)

我使用mocha作为测试框架,sinon用于模拟,存根和间谍。我建议您将您的帐户模块委派给auth.coffee模块,然后模仿它:

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中,但我不习惯。

答案 1 :(得分: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

答案 2 :(得分: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');
    });
});

答案 3 :(得分:0)

我推荐proxyquire

它完成了您想要实现的目标,而又不依赖于依赖项注入,这对您的代码来说很麻烦,并且如果您不以这种方式编写模块,则需要更改代码。