简体   繁体   English

如何通过从 mongo db 检索到的 object 到 javascript 中的测试

[英]How to pass object retrieved from mongo db to the test in javascript

I am working on NodeJS test automation project.我正在研究 NodeJS 测试自动化项目。 My goal is to get data from Mongo DB and use it in the tests.我的目标是从 Mongo DB 获取数据并在测试中使用它。 I successfully query the data from the DB, but so far I've failed to transfer the values to the test.我成功地从数据库中查询了数据,但到目前为止我还没有将这些值传输到测试中。

My Mongo DB halper (mongodbHelper.js):我的 Mongo DB halper (mongodbHelper.js):

var MongoClient = require('mongodb').MongoClient;
var url = "myurl";

var data;
const getTestData =  async function(){
    MongoClient.connect(url, function(err, db) {
        if (err) throw err;
        var dbo = db.db("mydb");
        dbo.collection("my_test_collection").findOne({"testId": "test_01"}, function(err, result) {
            if (err) throw err;
            db.close();
            data= result;
            console.log(data); // This print prints correct data
            return result;
        });
    });
}

module.exports =
{
    getTestData
}

The print inside the handler prints retrieved data correctly.处理程序内的打印正确打印检索到的数据。 Now I need to use it in the test suite below.现在我需要在下面的测试套件中使用它。 I want to initiate the testData variable in Before method and use it in tests.我想在 Before 方法中启动 testData 变量并在测试中使用它。 If I do it as below, the print in the test prints undefined.如果我按以下方式进行操作,则测试中的打印打印未定义。 It prints it before the correct print in the helper.它在帮助程序中正确打印之前打印它。 So I assume it is a synchronisation problem.所以我认为这是一个同步问题。

const mongodb = require('./mongodbHelper');
var testData; 

    describe('Playwright with Mocha Demo', function() {
    
        before('name', async function() {
            testData = await mongodb.getTestData();
            console.log(testData); // Here it prints 'undefined'

        });
    ....

Please advise how I can pass the data from the handler to the testData variable in the test correctly.请告知我如何将数据从处理程序正确传递到测试中的 testData 变量。

You aren't returning anything from your async function getTestData .您没有从异步 function getTestData返回任何内容。

The Mongo call is asynchronous but you're using a callback function. Mongo 调用是异步的,但您使用的是回调 function。 You could pass a callback function to getTestData and call it with the data (where you're doing the console log).您可以将回调 function 传递给getTestData并使用数据调用它(您正在执行控制台日志)。 However, the Mongo calls return Promises, so you could just do something like this:然而,Mongo 调用返回 Promises,所以你可以这样做:

const getTestData = async function() {
    const db = await MongoClient.connect(url);
    const dbo = db.db("mydb");
    const result = await dbo.collection("my_test_collection").findOne({"testId": "test_01"});
    db.close();
    return result;
});

Then in your test you can retrieve the data with:然后在您的测试中,您可以使用以下方法检索数据:

testData = await mongodb.getTestData();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将从一个量角器测试脚本中检索到的变量传递给另一个 - How to pass a variable retrieved from 1 protractor test script to another Javascript和mongo db,如何将条件数组传递给查询 - Javascript and mongo db , how to pass arrays of conditions to a query 如何在 Javascript 代码中显示 Laravel 从数据库中检索到的结果 - How to show Laravel retrieved results from the DB in a Javascript code 如何将从mysql检索到的数据传递到JavaScript中以在画布上绘制点 - How to pass data retrieved from mysql into a javascript for ploting dots on canvas 解码使用Javascript从PHP检索到的json对象 - Decoding json object retrieved from PHP in Javascript 将字符串解析为对象以从Mongo DB查询 - Parse string to object to query from mongo db 自动将检索到的值从javascript函数传递给另一个javascript函数? - Automatically pass the retrieved value from a javascript function to another javascript function? 使用socket.io或javascript访问器从数据库广播检索到的结果 - Broadcast retrieved results from DB with accessors to socket.io or javascript Mongo Db,如何查询参数中的 object - Mongo Db , how to query where in the paramer is an object 如何在javascript中访问从Recordset中取出的DB检索数据 - how to access DB retrieved data out of Recordset in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM