我有两个带有静态方法的ValidationHelper、BeneficiaryHelper类,每个类都试图使用proxyquire进行模拟,但在运行npm测试时却给了我错误:
TypeError:无法读取未定义的属性“checkMandatory”
类型记录文件代码:
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 }用于此文件的单元测试的代码:
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
});
//...
});发布于 2019-03-28 12:42:57
希望这能奏效!
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
});
//...
});https://stackoverflow.com/questions/55390569
复制相似问题