這是我編寫測試用例的函數。無法指定要在mocha nodejs中測試的函數拋出的錯誤。 expect.to.throw(錯誤)不起作用
exports.sendOTPParser = event => {
console.log('sendOTPParser');
event = event || {};
try {
const body = JSON.parse(event.body);
const phoneNumber = body.phonenumber;
if (typeof phoneNumber !== 'string') {
throw new Error('MalformedRequest');
}
event.phoneNumber = body.phonenumber;
return event;
} catch (error) {
console.log('Error in isEmailUniqueParser: %s', error.message);
throw new Error('MalformedRequest');
}
}
這是測試給定函數的摩卡密碼。但是,這不是錯誤。怎麼了??
var file = require('../src/auth/eventParsers');
var expect = require('chai').expect;
describe('sendOTPParserTest', function(){
var event1 = {"body": '{"phonenumber": 1234}'};
var event2 = {"body": '{"phonenumber": "1234"}'};
// event2.body.phonenumber = '12345';
// event1.body.phonenumber = 12345;
it('returnTest', function(){
var result1 = file.sendOTPParser(event1);
// var result2 = file.sendOTPParser(event2); expect(result2.phoneNumber).to.equal(JSON.parse(event2.body).phonenumber);
expect(result1).to.throw(Error);
});
});
輸出:
bash-3.2$ mocha sendOTPParserTest.js
sendOTPParserTest
sendOTPParser
Error in isEmailUniqueParser: MalformedRequest
1) returnTest
0 passing (14ms)
1 failing
1) sendOTPParserTest returnTest:
Error: MalformedRequest
at
輸出:慶典-3.2 $摩卡sendOTPParserTest.js sendOTPParserTest sendOTPParser 錯誤isEmailUniqueParser:MalformedRequest 1)returnTest 0合格(14ms) 1失敗 1)sendOTPParserTest returnTest: 錯誤:MalformedReque在Context處的Object.exports.userCreateEventParser.exports.sendOTPParser.event [as sendOTPParser](/Users/akshayrelfor/Documents/curber-dir/src/auth/eventParsers.js:40:11) 處的st 。(sendOTPParserTest.js:15:24) –