将数据存储在一个变量中,以便在运行frisby测试之前在REST API路由中作为参数使用

Store data in a var to use in REST API routes as params before running frisby tests

本文关键字:REST API 测试 参数 frisby 路由 存储 数据 一个 变量 运行      更新时间:2023-09-26

我有使用参数(电子邮件和令牌)的路由,我想在frisby测试中通过它们,但在运行测试之前,我必须在DB (mongo)中获取这些数据。

问题是我的测试运行(或不)之前,我得到的数据。

代码如下:

/* global require */
"use strict";
var frisby = require('frisby');
var emailUser;
var tokenUser;
var getDataUser = function(e) {
    var MongoClient = require('mongodb').MongoClient;
    MongoClient.connect('mongodb://localhost/virtusBack-end', function(err, db) {
        if (err) throw err;
        console.log("Connected to Database");
        var user = db.collection('users').findOne({
            name: "virtus-app"
        });
        user.then(function(result) {
            emailUser = result.email;
            tokenUser = result.token1 + result.token2;
            db.close(test(emailUser, tokenUser));
        });
    });
}
function test(email, token){
        console.log(email);
//http://localhost:8080/rooms?email=""?token="" (IT HAS 2 PARAMS, EMAIL ADN USER TOKEN)
	frisby.create('Test DB').get('http://localhost:8080/rooms')
	    .expectStatus(200)
	    .toss();
	frisby.create('Test DB 2').get('http://localhost:8080/rooms')
	    .expectStatus(200)
	    .expectHeaderContains('content-type', 'application/json')
	    .toss();
	frisby.create('Creating Room: 09')
	    .post('http://localhost:8080/rooms', {
	        _id: "000000000000000000000002",
	        number: 9,
	        floor: 0
	    })
	    .expectStatus(200)
	    .toss();
}
getDataUser();

函数test()应该在db关闭后运行,但我得到以下结果:

0秒完成0个测试、0个断言、0个失败、0个跳过连接数据库virtus-app.com

测试结果应该出现在console.log输出之后。我认为测试没有运行,如果它们运行,那也是在我获得数据之前。

有什么帮助吗

我们在frisbyjs中遇到了完全相同的问题,缺少像beforeafter这样的BDD钩子来收集数据并清除测试中创建的任何内容。

我最终创建了Chakram (https://github.com/dareid/chakram),它提供了BDD钩子并利用承诺,因此可以轻松编写更复杂的测试。