如何在所有测试文件之前运行Jasmine [英] How to run Jasmine beforeAll for all test files

查看:129
本文介绍了如何在所有测试文件之前运行Jasmine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nodejs为我们的API创建端到端测试套件。在每次测试运行之前,我需要为该测试插入数据库记录。许多表都没有使用原生自动增量类型字段作为主键。

I'm using nodejs to create an end to end test suite for our API. Before each test runs, I need to insert database records for that test. Many of the tables in question do not use native auto-increment type fields for their primary keys.

(我知道,数据库设计不好。但我没有控制权那个。)

(I know, bad database design. But I don't have control over that.)

相反,它们使用postgresql常用的序列模式。但这是在MS SQL Server中。所以有一个存储过程可以获取下表中使用的序列号等。

Instead, they use the sequence pattern common to postgresql. But this is in MS SQL Server. So there is a stored procedure that gets the next sequence number for use in the table, etc.

当我开始设置我的测试时,我发现我的交易由于javascript的异步性质,它们相互冲突。基本上,事务获得相同的序列号,然后尝试相互提交。因此,独特的约束失败。

As I begin to set up my tests, I am finding that my transactions are colliding with each other because of the asynchronous nature of javascript. Basically, the transactions are getting the same sequence numbers and then trying to commit on top of each other. So unique constraints are failing.

我想到的第一个解决方案(我向其他人开放)就是在任何测试之前设置所有数据库记录跑。但据我所知,Jasmine的beforeAll()函数仅适用于它所在的文件。我需要一个beforeAll()函数,该函数在所有文件中的所有文件运行之前运行。

The first solution that comes to my mind (I'm open to others) is to just set up all of the database records before any tests run. But to my knowledge, Jasmine's beforeAll() function only applies to the file that it's in. I need a beforeAll() function that runs before all Jasmine tests run in all files everywhere.

Jasmine有类似的东西吗?如果没有,有没有办法在nodejs中创建一个控制器,它将在数据库中设置测试用例,然后以编程方式生成jasmine?

Does Jasmine have something like that? If not, is there a way I can create a controller in nodejs that will set up the test cases in the database and then spawn jasmine programatically?

提前致谢!

推荐答案

看起来Jasmine可以通过编程方式由nodejs生成。我找到了使用以下代码执行此操作的方法:

It looks like Jasmine can be spawned by nodejs programmatically. I found a way to do this with the following code:

... Do database setup stuff here ...

.then(() => {
    const Jasmine = require('jasmine');
    const jasmine = new Jasmine();

    jasmine.loadConfig({
        spec_dir: 'spec'
        ,spec_files: [
            '**/*[Ss]pec.js'
        ]
        ,helpers: [
            'helpers/**/*.js'
        ]
        ,random: false
    });

    jasmine.execute();
});

这样可行,因为我可以将此代码放入promise.then()回调中并在之后执行我已完成数据库设置工作。

This will work because I can put this code into a promise.then() callback and execute it after I have done database set-up work.

这篇关于如何在所有测试文件之前运行Jasmine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆