我已经使用 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;
任何关于函数运行原因但测试失败原因的见解将不胜感激。
Ps 错误如下:
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)