英文:
How to test a method which returns data from AWS DynamoDB
问题
I have created a new API endpoint in API Gateway,映射了我的AWS Lambda函数,并想为处理请求的JavaScript方法编写测试用例。该方法使用aws-sdk节点模块创建与AWS DynamoDB的连接,将传递的值保存到数据库,并返回一个带有相关数据的Promise
。我不希望spec触发对实际DynamoDB的调用或创建新条目。我尝试使用存根,但仍然出现错误,指示测试用例失败。
Handler.js:
saveUser(userName, logger) {
const Item = {
id: uuid.v4(),
userName,
ttl: parseInt(Date.now() / 1000) + 900 // 从现在起的15分钟后过期
};
const params = {
TableName: "my-table-name"
};
logger.log(`将新用户名保存到DynamoDB:${JSON.stringify(params)}`);
return new Promise(function(resolve, reject) {
db.put(params, function(err, _) {
if (err) {
logger.exception(`无法连接到DynamoDB进行创建:${err}`);
reject({
statusCode: 404,
err
});
} else {
logger.log(`已将数据保存到DynamoDB:${JSON.stringify(Item)}`);
resolve({
statusCode: 201,
body: Item
});
}
});
});
}
Handler.spec.js:
import AWS from "aws-sdk";
const db = new AWS.DynamoDB.DocumentClient({
apiVersion: "2012-08-10"
});
describe("user-name-handler", function() {
const sandbox = sinon.createSandbox();
afterEach(() => sandbox.restore());
it("测试saveUser()方法", async function(done) {
const { saveUser } = userHandler;
sandbox.stub(db, "put")
.returns(new Promise((resolve, _) => resolve({
statusCode: 200
})));
try {
const result = await saveUser("示例用户", {
log: () => {},
exception: () => {}
});
expect(result).to.be.equal({
data: "一些数据"
});
done();
} catch (err) {
console.log(err);
done();
}
});
});
错误:
错误:解析方法过于具体。指定回调*或*返回Promise;不要同时使用两者。
英文:
I have created a new API endpoint in API Gateway mapped my AWS Lambda function and want to write test cases for the JavaScript method which handles requests made to the endpoint. The method creates a connection to AWS DynamoDB using the aws-sdk node module to save passed values to the database and returns a Promise
with relevant data. I do not want the spec to trigger a call to the actual DynamoDB or create a new entry to it. I tried using a stub, but it still gave me an error stating that the test-case failed.
Handler.js:
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-js -->
saveUser(userName, logger) {
const Item = {
id: uuid.v4(),
userName,
ttl: parseInt(Date.now() / 1000) + 900 // expire the name after 15 minutes from now
};
const params = {
TableName: "my-table-name"
};
logger.log(`Saving new user name to DynamoDB: ${JSON.stringify(params)}`);
return new Promise(function(resolve, reject) {
db.put(params, function(err, _) {
if (err) {
logger.exception(`Unable to connect to DynamoDB to create: ${err}`);
reject({
statusCode: 404,
err
});
} else {
logger.log(`Saved data to DynamoDB: ${JSON.stringify(Item)}`);
resolve({
statusCode: 201,
body: Item
});
}
});
});
}
<!-- end snippet -->
Handler.spec.js:
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-js -->
import AWS from "aws-sdk";
const db = new AWS.DynamoDB.DocumentClient({
apiVersion: "2012-08-10"
});
describe("user-name-handler", function() {
const sandbox = sinon.createSandbox();
afterEach(() => sandbox.restore());
it("Test saveUser() method", async function(done) {
const {
saveUser
} = userHandler;
sandbox.stub(db, "put")
.returns(new Promise((resolve, _) => resolve({
statusCode: 200
})));
try {
const result = await saveUser("Sample User", {
log: () => {},
exception: () => {}
});
expect(result).to.be.equal({
data: "some data"
});
done();
} catch (err) {
console.log(err);
done();
}
});
});
<!-- end snippet -->
Error:
Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.
答案1
得分: 1
尝试将 function(done)
替换为 function()
并移除所有对 done()
的调用;
英文:
Try to replace function(done)
with function()
and remove all the calls to done()
;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论