【问题标题】:DynamoDB code not working inside of Mocha test case?DynamoDB 代码在 Mocha 测试用例中不起作用?
【发布时间】:2019-06-29 03:32:05
【问题描述】:

背景: 我创建了一个 Dynamo DB 表并填充了它。我有一些代码可以读取名为 readItem.js 的文件中的项目,当我独立运行它时它可以工作,但当我将它作为 Mocha 测试用例运行时它不起作用。

知道为什么代码在 it() 测试用例中不能正常运行吗?

对于独立的,我这样运行应用程序:

node readItem.js

readItem.js:

var AWS = require("aws-sdk");

AWS.config.update({
  region: "us-west-2",
  endpoint: "http://localhost:8000"
});

var docClient = new AWS.DynamoDB.DocumentClient();
var table = "Movies";
var year = 2015;
var title = "The Big New Movie";
var params = {
    TableName: table,
    Key:{
        "year": year,
        "title": title
    }
};

docClient.get(params, function(err, data) {
    if (err) {
        console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
    }
});

但是,当我在 Mocha 测试用例中运行它时,不会调用 docClient.get() 调用上的回调函数,即:

docClient.get(params, function(err, data) { ...// Not being invoked });

这是我将代码构造为测试用例的方式。我调用测试:

npm run test

“test”在我的 package.json 中定义为:

"scripts": {
"test": "./node_modules/.bin/mocha --recursive --reporter spec --ui bdd test/"
}

测试用例代码:

const AWS = require('aws-sdk');

AWS.config.update({
  region: "us-west-2",
  endpoint: "http://localhost:8000"
});

var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
let table = "Movies";
let year = 2015;
let title = "The Big New Movie";
let params = {
    TableName: table,
    Key:{
        "year": year,
        "title": title
    }
};



describe('DynamoDB Tests', function() {

    it("Read Item", function() {

        try {
            docClient.get(params, function (err,data) {
                if (err) {
                    console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
                } else {
                    console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
                }               
            });
        } catch (err) {
            console.log("ERROR:"+ err);
        }

    });
});

【问题讨论】:

  • 尝试在 before() 挂钩中完成测试设置。

标签: amazon-dynamodb mocha.js aws-sdk aws-sdk-nodejs


【解决方案1】:

我使用的是旧版本的摩卡: “摩卡”:“^3.5.3”

升级解决了 “摩卡”:“^6.1.4”

【讨论】:

    猜你喜欢
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多