我有一个在AWS上运行的NodeJS lambda函数。我想为exports.bidNotification = functions.database.ref('/notification/{pushId}').onWrite((event) => {
const data = event.data;
console.log('Notification received');
if(!data.changed()){
console.log('Nothing changed');
return;
}
const payLoad = {
notification:{
title: 'App name',
body: data.val().message,
sound: "default"
}
};
const options = {
priority: "high",
timeToLive: 60*60
};
return admin.messaging().sendToDevice(data.val().token, payLoad, options);
});
函数编写一个简单的测试。
CODE
以下是.handler
代码:
index.js
如果调用// importing dependencies
var mySQLWriter = require('./mySQLWriterService');
exports.handler = function(event, context, callback) {
console.log('Printing out JSON.stringify(event): ');
console.log(JSON.stringify(event));
event.Records.forEach((record) => {
if (record.eventName === 'INSERT') {
console.log('We have an INSERT happening.');
mySQLWriter(record, callback);
}
});
};
,我想写一个简单的测试。
使用Mocha和Chai,并在下面的dashmud的帮助下,我试图这样做,但它不起作用,这是我的mySQLWriter
代码:
indexTests.js
当我进行测试时,我得到:
答案 0 :(得分:1)
删除 chai 并使用 sinon
//const chai = require('chai');
//const expect = chai.expect;
const sinon = require('sinon');
//chai.use(sinon);
const SQLWriter = require('./mysqlwriterservice.js');
const appStart = require('./sinonsqlwriter');
describe('lambda function', () => {
it('should call the mySQLWriter() function', () => {
const spy = sinon.spy(SQLWriter, 'mySQLWriter');
let event = {
Records: [
{
eventName: 'INSERT',
dynamodb: {
NewImage: {
DeviceId: { S: 'Device 000' },
TimeStamp: { S: '2018-03-20T11:15:31.668Z' },
Accuracy: { S: '5' },
Latitude: { S: '53.639645' },
Longitude: { S: '-1.782491' },
Speed: { S: '1' },
}
}
}
]
};
const context = {};
appStart.handler(event, context, () => {
console.log("Call count"+spy.callCount)
//expect(spy).to.have.been.called();
})
});
});
// sinonsqlwriter.js
//导入依赖项
const SQLWriter = require('./mysqlwriterservice.js');
exports.handler = function(event, context, callback) {
console.log('Printing out JSON.stringify(event): ');
console.log(JSON.stringify(event));
event.Records.forEach((record) => {
if (record.eventName === 'INSERT') {
console.log('We have an INSERT happening.');
SQLWriter.mySQLWriter(record, callback);
SQLWriter.mySQLWriter(record, callback);
}
});
callback();
};
// mysqlwriterservice.js 我使用了共享链接中的代码。以下是更新答案:
答案 1 :(得分:0)
您似乎没有调用处理程序函数,因此请尝试更改
appStart()
到
appStart.handler(someEvent)
答案 2 :(得分:0)
您的appStart
只是一个模块。您需要调用handler
(appStart
)内的index.js
并在回调中进行断言。
const chai = require('chai');
const expect = chai.expect;
const spies = require('chai-spies');
chai.use(spies);
const appStart = require('../index');
describe('lambda function', () => {
it('should call the mySQLWriter() function', done => {
const spy = chai.spy.on(mySQLWriter, 'mySQLWriter');
const event = {};
const context = {};
appStart.handler(event, context, () => {
expect(spy).to.have.been.called();
done();
})
});
});
根据评论和更新的问题进行更新:
根据您的屏幕截图,似乎mySQLWriterService.js
导出一个具有mySQLWriter
功能的对象。
这不起作用。
var mySQLWriter = require('./mySQLWriterService');
我认为应该是这样的:
const mySQLWriter = require('./mySQLWriterService').mysqlWriter;
(由于您未在mySQLWriterService.js
中添加代码,我不能100%确定。)
P.S。不要使用var
。如初。