mocha 在文件之间共享变量 [英] mocha share variables between files

查看:50
本文介绍了mocha 在文件之间共享变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to wire the objects between 2 mocha test files. Here is my test1.js file which should export a variable once all the test cases are executed.

var assert = require('assert');


var newUser = {
    email: "test@ex.com",
    name: "test@ex.com",
    password: "test@ex.com",
    confirmPassword: "test@ex.com"
}


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 文件中导入对象

Here is my test2.file which imports the object from test1.js file

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 文件中得到的输出是

The output i get in test2.js file is

TestUserObj

    ::::{
        "testUser": {
            "loggedInUser": {},
            "avaialbleAssessment": {},
            "interests": {},
            "testSummary": {},
            "newUser": {
                email: "test@ex.com",
                name: "test@ex.com",
                password: "test@ex.com",
                confirmPassword: "test@ex.com"
            },
            "test": {}
        }
    }

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

the exported values does not not contain the modified objects

推荐答案

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

As @sheplu mentioned above in the comments, test files in unit testing should be separate. In fact, each individual unit being tested should be independent of other units.

您在案例中寻找的是buildupteardown 系统,或者fixtures.

What you are looking for in your case, is a buildup and teardown system, or fixtures.

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

Basically, you need to make sure that you have the required items already set up before you run a set of tests.

  • 创建一组虚拟数据,并快速将其播种到您的数据库中
  • 实现一种模拟登录用户的方法
  • 利用 Mocha 的 before() 和/或 beforeEach() 挂钩 来做到这一点
  • 如果您将 MongoDB 与 mongoose 一起使用,mockgoose 提供在内存中模拟测试数据库的优秀包
  • Create a set of dummy data, and quickly seed it into your database
  • Implement a way to simulate logged in user
  • Make use of Mocha's before() and/or beforeEach() hooks to do this
  • If you are using MongoDB with mongoose, mockgoose provides an excellent package to simulate a test database in memory

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

After test cases are done, you should also take care to:

  • 注销用户
  • 从数据库中删除数据
  • 使用 Mocha 的 after() 和/或 afterEach() 钩子
  • Log out the user
  • Remove the data from db
  • Use Mocha's after() and/or afterEach() hooks

您也可以查看 supertest 来进行 API 请求在测试用例中调用.

You can also check out supertest to make API request calls in test cases.

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

Doing the above in your second file would ensure that you always have a working set of data to run your tests with, within specific test suite.

这篇关于mocha 在文件之间共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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