节点功能可以工作,但在通过Mocha运行时未定义



我已经使用Twilio的API设置了一个工作函数。当我通过UI输入参数时,该函数有效,但当我在Mocha中运行测试时,它失败了,说明该函数未定义。我在Mocha还有一个测试,在这个测试之前运行,第一个测试通过了。

twilioClient.js:

var config = require('./config');
var client = require('twilio')(config.accountSid, config.authToken);
sendSms = function(to, message) {
  client.messages.create({
    body: message,
    to: to,
    from: config.sendingNumber
    // mediaUrl: 'http://www.yourserver.com/someimage.png'
  }, function(err, data) {
    if (err) {
      console.error('Could not notify administrator');
      console.error(err);
      return 'Could not notify administrator';
    } else {
      console.log('Administrator notified');
      return 'Administrator notified';
    }
  });
};
module.exports.sendSms = sendSms;

我的indexSpec.js文件:

var chai= require('chai');
var expect = require('chai').expect;
var twilioNotifications = require('./Send Text/js/twilioNotifications');
var cfg = require('./Send Text/js/config.js');
var twilio = require('twilio');
var tClient = require('./Send Text/js/twilioClient.js');
describe('TwilioVars', function() {
    it('returns true if variables are not null nor undefined', function() {
        var tVars = cfg.twilioVars(cfg.reqConfig);
        expect(tVars).to.equal(true);
    })
})
describe('MsgSent', function() {
    it('confirms if message can be sent', function() {
        var receiver = <my number>;
        var message = 'My message';
        var sentMsg = tClient.sendSms(receiver, message);   
        expect(sentMsg).to.equal('Administrator notified');
    })
})

我的config.js文件:

var cfg = {};
cfg.accountSid = <some string>;
cfg.authToken = <some string>;
cfg.sendingNumber = <some string>;
var requiredConfig = [cfg.accountSid, cfg.authToken, cfg.sendingNumber];
// For testing:
cfg.reqConfig = requiredConfig;
cfg.twilioVars = function(arr) {
    if (arr[0] && arr[1] && arr[2]) {
        return true;
    }
    return false;
};
// End testing
var isConfigured = requiredConfig.every(function(configValue) {
  return configValue || false;
});
if (!isConfigured) {
  var errorMessage =
    'TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, and TWILIO_NUMBER must be set.';
  throw new Error(errorMessage);
}
// Export configuration object
module.exports = cfg;

如果能深入了解为什么函数运行,但为什么测试失败,我们将不胜感激。

附言:错误如下:

 1) MsgSent confirms if message can be sent:
 AssertionError: expected undefined to equal 'Administrator notified'
  at Context.<anonymous> (C:UsersJames Bradleycode_teststwiliotwilio_testindexSpec.js:22:22)
  at callFn (C:UsersJames BradleyAppDataRoamingnpmnode_modulesmochalibrunnable.js:326:21)
  at Test.Runnable.run (C:UsersJames BradleyAppDataRoamingnpmnode_modulesmochalibrunnable.js:319:7)
  at Runner.runTest (C:UsersJames BradleyAppDataRoamingnpmnode_modulesmochalibrunner.js:422:10)
  at C:UsersJames BradleyAppDataRoamingnpmnode_modulesmochalibrunner.js:528:12
  at next (C:UsersJames BradleyAppDataRoamingnpmnode_modulesmochalibrunner.js:342:14)
  at C:UsersJames BradleyAppDataRoamingnpmnode_modulesmochalibrunner.js:352:7
  at next (C:UsersJames BradleyAppDataRoamingnpmnode_modulesmochalibrunner.js:284:14)
  at Immediate._onImmediate (C:UsersJames BradleyAppDataRoamingnpmnode_modulesmochalibrunner.js:320:5)

您的sendSms函数可能是异步的,您需要等待它完成后再测试它的输出。考虑向该函数添加一个回调,当client.messages.create()进行回调时可以调用该函数,并使用done。或者考虑返回sendSms的承诺。

例如:

twilioClient.js:

...
sendSms = function(to, message, cb) {
  client.messages.create({
    ...
  }, function(err, data) {
    ...
    cb(err, data);
  });
};

indexSpec.js:

...
describe('MsgSent', function() {
  it('confirms if message can be sent', function(done) {
    ...
    var sentMsg = tClient.sendSms(receiver, message, function(err, data) {
      expect(data.sentMsg).to.equal('Administrator notified');
      done();
    });
  });
});

最新更新