我尝试模拟一个模块Database
,这样它的成员函数getRecordDetails()
总是返回一些固定的数据。
处理程序.js
const sinon = require("sinon");
const Database = require("../../lib/Database");
const database= new Database();
module.exports = {
handler(event, context, callback) {
event.Records.forEach(record=> {
const recordDetails = database.getRecordDetails(record);
// Do something with recordDetails
});
}
}
..
测试.js
const sinon = require("sinon");
const db= require("../../lib/Database");
const lamdbda = require("../../lib/lambda.js");
describe("Test suite", () => {
it("invoke lambda", done => {
lambda.handler(data, {}, (err, data) => {
sinon.stub(db, "getRecordDetails").withArgs(sinon.match.any).returns("{id: 1}");
...
}
}
}
database.js
class Database {
constructor() {
this.dynamdbClient = new AWS.DynamoDB.DocumentClient();
}
getRecordDetails(record) {
const param = {
TableName: ..
Key: {
uuid: record.uuid
...
}
}
const dbRecord = await docClient.get(params).promise();
return mapToRecordDetails(dbRecord); // new object mostly 1-1 mappings.
}
}
但我得到:
TypeError:无法存根不存在的自己的属性getRecordDetails我做错了什么?
1条答案
按热度按时间5uzkadbs1#
getRecordDetails
方法是Database
类的示例方法。请尝试sinon.stub(Database.prototype, 'getRecordDetails')
。例如
handle.js
:database.js
:handler.test.js
:试验结果: