1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Lambda(node.js)におけるDynamoDBのスタブのつくりかた

Last updated at Posted at 2020-08-31

#やりたいこと
以下のlambdaのテスト対象コード(main.js)をMochaでtestするときに
docClient.get(...).promise() 部分のスタブをsinonでつくりたい

main.js
const aws = require("aws-sdk");
const docClient = new aws.DynamoDB.DocumentClient(...);

async function main(event) {

    await docClient.get(...).primise();

}

結論のテストコード
(***で囲まれているところがスタブのつくり方、それ意外はモジュールの設定)

main_test.js
const assert = require('assert');
const sinon = require("sinon");
const aws = require("aws-sdk");
const docClient = new aws.DynamoDB.DocumentClient(...);


describe("main_test", () => {

  it("test1", async() => {

    // ******ここスタブのつくりかたでハマった******
    let stub_DB = sinon.stub(docClient,'get').returns({
        promise: function () {return Promise.resolve({data:111})
        });
    //stub_DBはdocClient.get(...).promies()が呼ばれると{date:111}をreturnするスタブ
    //***********************************

  });

});

#参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?