如何使用Mocha在Node测试中模拟Express响应对象

时间:2018-09-05 11:52:28

标签: node.js express mocking mocha response

我需要测试一个需要响应作为参数的函数,我正在使用mocha并在带​​有快速框架的节点环境中进行续集。我的代码被固定在多个文件中,但是我只需要测试superactivator距离。这些是我的文件:

route.js

const express = require('express');
const router = express.Router();

const rcv_pcController = require('../controllers/rcv_pc.controller');

router.post('/', rcv_pcController.switchMode);

module.exports = router;

rcv_pc.controller.js

const superActivator = require('../helpers/superactivator');
...

const switchMode = async (req, res) => {
    ...

    switch (tipo) {
        case "1":
            {
                if (modo == "1") {
                    superActivator.checkLicense(license, hwId, oem, expire, nowDate, ip, allowedSerials, res)
                }
                else if (modo == "2") {
                    superActivator.generateLicense(license, hwId, reqCode, nowDate, ip, res);
                }
                else if (modo == "3") {
                    superActivator.registerLicense(license, hwId, reqCode, nowDate, customerName, referenteName, referentePhone, ip, res)
                }
                break;
            }
    }
}

module.exports.switchMode = switchMode;

superactivator.js

const pcRepo = require("../repositories/pc.server.repository");
const repository = require('../repositories/rcvpc.server.repository');
...

class SuperActivator {

    ...
    checkLicense(license, hwId, oem, expDate, nowDate, ip, allowedSerials, res) {
        repository.findLicense(license).then(key => {
            // console.log(key[0]);
            if (key[0]) {
                if (!isset(key[0]['SS_ALLOWED_SERIALS']) || is_null(key[0]['SS_ALLOWED_SERIALS'])) {
                    key[0]['SS_ALLOWED_SERIALS'] = "";
                }
                if (this.updatePcRx(hwId, ip, nowDate) == 0) {
                    return res.send(this.licCheckResult.server_error);
                }
                if (this.checksetBanned(hwId) == 0) {
                    return res.send(this.licCheckResult.hwid_banned);
                }
                if (!isset(key[0]['SP_HW_ID'])) {
                    return res.send(this.licCheckResult.key_virgin);
                }
                if (key[0]['SS_STATUS'] < 1) {
                    return res.send(this.licCheckResult.key_unallowed);
                }
                if (key[0]['SP_HW_ID'] != hwId) {
                    if (this.setKeyMismatched(key[0]['SS_ID']) == 1) {
                        return res.send(this.licCheckResult.key_moved);
                    }
                }
                if ((strtotime(key[0]['SS_EXPIRE']) < strtotime(expDate))
                    || (strtotime(nowDate) < strtotime(key[0]['SP_PC_DATE_TIME']))
                    || (strtotime(nowDate) < time() - 60 * 60 * 24 * 2)) {
                    if (this.setKeyMismatched(key[0]['SS_ID']) == 1) {
                        return res.send(this.licCheckResult.dates_hacked);
                    } else {
                        return res.send(this.licCheckResult.server_error);
                    }
                }
                if ((key[0]['SS_OEM'] != oem)
                    || (strtotime(key[0]['SS_EXPIRE']) > strtotime(expDate) || strcmp(key[0]['SS_ALLOWED_SERIALS'], this.decodeToMortal(allowedSerials)) != 0)) {
                    return res.send(this.licCheckResult.key_info_to_update);
                }
                if (strtotime(key[0]['SS_EXPIRE']) <= strtotime(nowDate)) {
                    return res.send(this.licCheckResult.key_expired);
                }
                return res.send(this.licCheckResult.key_ok);
            } else {
                return res.send(this.licCheckResult.key_insesistente);
            }
        }).catch(err => res.send(err.errors));
    }

    generateLicense(license, hwId, reqCode, nowDate, ip, res) {
        pcRepo.updatePcRx(hwId, ip, nowDate);

        repository.findOem(license, hwId)
            .then((foundOem) => {
                if (foundOem[0]) {
                    const keyCode = this.generateValidKey(this.decodeToMortal(reqCode));
                    let patchKey = keyCode;
                    if (keyCode.length != 10 || this.checkValidKey(keyCode, patchKey) == 'KO') {
                        return res.send(this.licCheckResult.invalid_reqcode);
                    } else {
                        let oem = '';
                        switch (foundOem[0]['SS_OEM']) {
                            case 0:
                                oem = 'thisisnotoem'
                                break;
                            case 1:
                                oem = 'thisisoem'
                                break;
                            case 2:
                                oem = 'thisisoemdoc'
                                break;
                            case 3:
                                oem = 'thisislock'
                                break;
                            case 10:
                                oem = 'thisisnotoem_lecu'
                                break;
                            case 11:
                                oem = 'thisisdemo_lecu'
                                break;
                            case 12:
                                oem = 'thisisoem_lecu'
                                break;
                        }

                        const keepDate = str_replace('-', "", foundOem[0]['SS_EXPIRE'])
                        const allowedSerials = this.getAllowedSerials(foundOem[0]['SS_ID'])
                        console.log(allowedSerials);
                        const key =
                            this.codeToGod(keyCode) + '|'
                            + this.codeToGod(patchKey) + '|'
                            + this.codeToGod(oem) + '|'
                            + this.codeToGod(keepDate) + '|'
                            + this.codeToGod(allowedSerials);
                        // console.log(key);
                        return res.send(key);
                    }
                } else {
                    return res.send(this.licCheckResult.key_insesistente);
                }
            }).catch(err => res.send(err.errors));
    }

    registerLicense(license, hwId, reqKey, pcDate, customerName, referenteName, referentePhone, ip, res) {
        pcRepo.findOne(hwId)
            .then((pc) => {
                let pcId = '';
                if (!pc) {
                    const data = {
                        SP_HW_ID: hwId,
                        SP_LAST_RX: Date.now(),
                        SP_IP: ip,
                        SP_PC_DATE_TIME: new Date().toISOString().slice(0, 10)
                    }
                    pcRepo.create(data)
                        .then((newPc) => {
                            if (!newPc) {
                                return res.send(this.licCheckResult.server_error);
                            }
                            return pcId = newPc['SP_ID']
                        }).catch(err => res.send(err.errors));
                } else {
                    pcId = pc['SP_ID']
                }
                console.log(pcId);
                if (isset(pcId) || pcId.length == 0 || pcId == 0) {
                    return res.send(this.licCheckResult.server_error);
                }
                repository.updateLicense(pcId, customerName, referenteName, referentePhone, license)
                    .spread((results, metadata) => {
                        if (!results) {
                            return res.send(this.licCheckResult.server_error);
                        }
                        return this.generateLicense(license, hwId, reqKey, pcDate, ip, res);
                    }).catch(err => res.send(err.errors))
            }).catch(err => res.send(err.errors));
    }

}

...

const superActivator = new SuperActivator(licCheckResult);

module.exports = superActivator;

为清楚起见,我忽略了所有Superactivator类方法。这是我的测试代码:

superactivator.spec.js

...

const superactivator = require('../helpers/superactivator');

...

describe('checkLicense()', function () {
    it('sks ', async function () {
        // here I mock the request.body data
        const ip = '127.0.0.1';
        const license = "A2YyLM8i3G7feJt7Hlm8hxlYk";
        const hwId = "123490EN40";
        const oem = 12;
        const expDate = "2019-10-10";
        const nowDate = null; // "2018-09-05"
        const allowedSerials = null;

        const foundSks = await superactivator.checkLicensecheckLicense(license, hwId, oem, expDate, nowDate, ip, allowedSerials, res)
        assert.equal(foundSks, '1');
    });
});

我得到了错误

  

ReferenceError:未定义res

     

在上下文中。 (test \ superactivator.spec.js:23:130)

因为我的测试文件中没有res。我怎么解决这个问题?我只需要模拟响应以检查返回的值即可。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您可以使用 Sinon 来帮助您侦察/存根res,例如:

...

const superactivator = require('../helpers/superactivator');
const sinon = require('sinon'); // ----> use sinon
...

describe('checkLicense()', function () {
    it('sks ', async function () {
        // here I mock the request.body data
        const ip = '127.0.0.1';
        const license = "A2YyLM8i3G7feJt7Hlm8hxlYk";
        const hwId = "123490EN40";
        const oem = 12;
        const expDate = "2019-10-10";
        const nowDate = null; // "2018-09-05"
        const allowedSerials = null;
        const res = { 
          send: sinon.spy() // --> create spy for res.send method
        }

        const foundSks = await superactivator.checkLicensecheckLicense(license, hwId, oem, expDate, nowDate, ip, allowedSerials, res)
        assert.equal(foundSks, '1');
    });
});

参考: Versioning and Deploying Salesforce Metadata using TFS/VSTS