mocha 在文件之间共享变量

2024-05-05

我正在尝试在 2 个摩卡测试文件之间连接对象。这是我的 test1.js 文件,一旦执行所有测试用例,该文件应该导出一个变量。

var assert = require('assert');


var newUser = {
    email: "[email protected] /cdn-cgi/l/email-protection",
    name: "[email protected] /cdn-cgi/l/email-protection",
    password: "[email protected] /cdn-cgi/l/email-protection",
    confirmPassword: "[email protected] /cdn-cgi/l/email-protection"
}


var studentAcademicData = {
    marks: {},
    activities: {}
}

var studentInterests = []

var testSummary = {},
    loggedInUser = {},
    avaialbleAssessment = {},
    test = {},
    interests = {};

var studentAcademicId, studentId, academicYearId, assessmentId, testId;



describe('perform functional Test', function() {

    before(function() {
        this.timeout(15000);
        db.init(config.mongodb);
    })

    //Register a student    it ('Register a student', function(done){

    StudentController.register(newUser).then(function(data) {
        assert.equal(data.name, newUser.name) assert.equal(data.tenant, newUser.tenant) assert.equal(data.customerType, newUser.customerType)

        done();
    }).catch(done)
});

//User authentication   it ('Authenticates user', function(done){


var userInfo = {
    appId: "abc",
    email: newUser.email,
    password: newUser.password
}

security.userAuthenticate(userInfo).then(function(data) {

securityToken = data.securityToken;
tenantId = data.tenantId;
emailStatus = data.emailStatus;
mobileStatus = data.mobileStatus studentId = data.userId;

done();
}).catch(done)
});


it('Gets Student by id', function(done) {

StudentController.getById(studentId).then(function(data) {

    loggedInUser = data;
    loggedInUser.tenantId = 'abc';
    loggedInUser.userId = studentId;
    loggedInUser.securityToken = securityToken;

    done();
}).catch(done)
});

})

module.exports.testUser = {
    loggedInUser: loggedInUser,
    avaialbleAssessment: avaialbleAssessment,
    interests: interests,
    testSummary: testSummary,
    studentAcademicData: studentAcademicData,
    newUs
    er: newUser,
    test: test
};

这是我的 test2.file,它从 test1.js 文件导入对象

var assert = require('assert');
var rewire = require('rewire');

var TestUserObj = require('./test1');


describe('perform test2 Test', function() {

    console.log("in test2")

    console.log("TestUserObj ::::" + JSON.stringify(TestUserObj))

});

我在 test2.js 文件中得到的输出是

TestUserObj

    ::::{
        "testUser": {
            "loggedInUser": {},
            "avaialbleAssessment": {},
            "interests": {},
            "testSummary": {},
            "newUser": {
                email: "[email protected] /cdn-cgi/l/email-protection",
                name: "[email protected] /cdn-cgi/l/email-protection",
                password: "[email protected] /cdn-cgi/l/email-protection",
                confirmPassword: "tes[email protected] /cdn-cgi/l/email-protection"
            },
            "test": {}
        }
    }

导出的值不包含修改的对象


正如@sheplu上面在评论中提到的,单元测试中的测试文件应该是分开的。事实上,被测试的每个单独单元应该独立于其他单元。

您在您的案例中寻找的是buildup and teardown系统,或fixtures https://en.wikipedia.org/wiki/Test_fixture#Software.

基本上,您需要确保在运行一组测试之前已经设置了所需的项目。

为此,您可以查看以下内容:

构建阶段:

  • 创建一组虚拟数据,并将其快速播种到数据库中
  • 实现模拟登录用户的方法
  • 利用摩卡咖啡before() and/or beforeEach() hooks https://mochajs.org/#hooks去做这个
  • 如果您将 MongoDB 与 mongoose 一起使用,mockgoose https://www.npmjs.com/package/mockgoose提供了一个优秀的包来模拟内存中的测试数据库

拆卸阶段:

测试用例完成后,您还应该注意:

  • 注销用户
  • 从数据库中删除数据
  • 使用摩卡咖啡after() and/or afterEach() hooks https://mochajs.org/#hooks

您还可以查看supertest https://www.npmjs.com/package/supertest在测试用例中进行 API 请求调用。

在第二个文件中执行上述操作将确保您始终拥有一组工作数据来在特定的测试套件中运行测试。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

mocha 在文件之间共享变量 的相关文章