首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不坚持静态方法的proxyquire . nodejs

不坚持静态方法的proxyquire . nodejs
EN

Stack Overflow用户
提问于 2019-03-28 05:11:09
回答 1查看 445关注 0票数 0

我有两个带有静态方法的ValidationHelper、BeneficiaryHelper类,每个类都试图使用proxyquire进行模拟,但在运行npm测试时却给了我错误:

TypeError:无法读取未定义的属性“checkMandatory”

类型记录文件代码:

代码语言:javascript
复制
import { ValidationHelper } from '../validations/common';
import { BeneficiaryHelper } from '../validations/beneficiary';
const lib = nbind.init<typeof LibTypes>(__dirname + '/../../').lib;



class beneficiaryaddv2 {

    utilities: any = {};

    constructor() {
        this.utilities = new lib.Utilities();
    }


    parse(req: any, res: any, message: { [k: string]: any }) {

        //..more code

        ValidationHelper.checkMandatory(req.body.beneficiaryType, 'beneficiaryType');
        ValidationHelper.checkMandatory(req.body.customerId, 'customerId');
        BeneficiaryHelper.checkBeneficiaryType(req.body.beneficiaryType);

        message.RESERVED1 = req.body.city;
        //..more code
    }

}

export { beneficiaryaddv2 }

用于此文件的单元测试的代码:

代码语言:javascript
复制
    class BeneficiaryHelper {
    static checkBeneficiaryType(beneficiaryType: string) { return; }
}

    class ValidationHelper {
        static checkMandatory(stringValue: string, parameterName: string, errorMessage: string = '') { return; }
    }


describe('unit test for beneficiary add parse', () => {

let utilBase;
let utilGenerateRRNMock;
let utilGenerateSTANMock;

let target = common.proxyquire('../../APIServer/controller/beneficiaryaddv2', {
    'nbind': common.nbindStub,
    '../../local_modules/logger': common.LoggerMock,
    '../validations/common': ValidationHelper,
    '../../local_modules/dbconnmgr': common.DbConnMgrMock,
    '../validations/beneficiary': BeneficiaryHelper,
    '@noCalThrough': true
});

//...

});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-28 12:42:57

希望这能奏效!

代码语言:javascript
复制
let ValidationHelperMock = {
  ValidationHelper: class{
    static checkMandatory(stringValue, parameterName, errorMessage){ };
  }
};

let BeneficiaryHelperMock = {
  BeneficiaryHelper: class{
    static checkBeneficiaryType(beneficiaryType){ };
  }
};

describe('unit test for beneficiary add parse', () => {

let utilBase;
let utilGenerateRRNMock;
let utilGenerateSTANMock;

let target = common.proxyquire('../../APIServer/controller/beneficiaryaddv2', {
    'nbind': common.nbindStub,
    '../../local_modules/logger': common.LoggerMock,
    '../validations/common': ValidationHelperMock,   //check these paths to exact from the test file
    '../../local_modules/dbconnmgr': common.DbConnMgrMock,
    '../validations/beneficiary': BeneficiaryHelperMock,   //check these paths to exact from the test file
    '@noCalThrough': true
});

//...

});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55390569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档