我已经使用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;
任何关于函数运行原因以及测试失败原因的见解都将非常感激。
P.S。错误如下:
1) MsgSent confirms if message can be sent:
AssertionError: expected undefined to equal 'Administrator notified'
at Context.<anonymous> (C:\Users\James Bradley\code_tests\twilio\twilio_test\indexSpec.js:22:22)
at callFn (C:\Users\James Bradley\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:326:21)
at Test.Runnable.run (C:\Users\James Bradley\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:319:7)
at Runner.runTest (C:\Users\James Bradley\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:422:10)
at C:\Users\James Bradley\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:528:12
at next (C:\Users\James Bradley\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:342:14)
at C:\Users\James Bradley\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:352:7
at next (C:\Users\James Bradley\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:284:14)
at Immediate._onImmediate (C:\Users\James Bradley\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:320:5)
答案 0 :(得分:2)
您的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();
});
});
});