2017-01-30 55 views
0

我只是在摩卡起步调用,我挣扎推测这一个。如何测试一个节点模块中的功能是用摩卡

比方说,我有这个节点的应用程序(app.js):

var myModule = require('./myModule'); 
function startingPoint() { 
    myModule.myFunction(); 
} 

,我有一个模块(myModule.js):

exports.myFunction = function() { 
    console.log('hello, world'); 
} 

现在,想什么,我要do是测试app.js并验证当我调用函数startingPoint时,myModule.myFunction被调用。我会如何去摩卡咖啡馆呢?

谢谢!

+1

您可以使用柴。看[这里](http://stackoverflow.com/questions/26704677/how-do-i-test-if-a-function-calls-a-specific-method-function) –

+0

这将是很难称之为'起点'只要你在app.js之外点出口:) –

回答

-1

让我们考虑与摩卡咖啡,湾仔及chai-spy的方式。我已经出口startingPoint有权访问它进行测试。

"use strict" 

const chai = require('chai') 
const expect = chai.expect 
const spies = require('chai-spies') 
chai.use(spies); 
const startingPoint = require('../app') 
const myModule = require('../myModule') 

describe('App',() => { 

    context('.startingPoint()',() => { 

     it('doesn\'t call .myFunction()',() => { 
      let spy = chai.spy.on(myModule, 'myFunction') 
      expect(spy).not.to.have.been.called() 
     }); 

     it('calls .myFunction()',() => { 
      let spy = chai.spy.on(myModule, 'myFunction') 
      startingPoint() 
      expect(spy).to.have.been.called() 
     }); 

    }); 

}); 

output from mocha test