我有两个类 ValidationHelper, BeneficiaryHelper,每个类都有一个静态方法,我试图使用 proxyquire 来模拟它,但是在运行 npm test 时它给了我错误:
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
});
//...
});