尝试为changeFunctionalityStatus()编写单元测试。面临像 fControl.get 这样的问题不是一个函数。 坚持这个任何人请帮助我为什么会出现这个问题。 TIA
changeFunctionalityStatus(fControl: FormControl, screen) {
if (this.isConsult) return;
const isFuncActivated = fControl.get('isFunctionalityActivatedController').value;
if (fControl.value.functionalityCodeController == "CONSULT_PROJECT" || fControl.value.functionalityCodeController == "CONSULT_BUSINESS_OFFER" || fControl.value.functionalityCodeController == "CONSULT_PROJECT_CONTRACT" || fControl.value.functionalityCodeController == "CONSULT_BILLING_LIST" || fControl.value.functionalityCodeController == "CONSULT_PROJECT_INVOICES" || fControl.value.functionalityCodeController == "CONSULT_CONTRACT" || fControl.value.functionalityCodeController == "CONSULT_MILESTONE") {
this.removeFunctionalitiesByScreen(screen);
}
if (isFuncActivated) {
this.removeFunctionality(fControl);
} else {
this.addFunctionality(fControl);
}
this.checkAndSetScreenAccessController(screen);
}
规格ts
it('changeFunctionalityStatus', () => {
spyOn(component, 'changeFunctionalityStatus').and.callThrough();
component.changeFunctionalityStatus({} as FormControl,"mockscreen");
expect(component.changeFunctionalityStatus).toHaveBeenCalled();
});
您应该返回一个模仿
fControl
中的属性和方法的模拟对象。每次运行新的测试用例时,我都会使用 beforeEach
重置属性。
describe('changeFunctionalityStatus method', () => {
let fControl: any = {};
let returnVal = {
value: true,
};
beforeEach(() => {
fControl = {
get: () => returnVal,
value: {
functionalityCodeController: 'CONSULT_MILESTONE',
},
};
spyOn(component, 'removeFunctionalitiesByScreen');
spyOn(component, 'removeFunctionality');
spyOn(component, 'addFunctionality');
spyOn(component, 'checkAndSetScreenAccessController');
});
it('should call removeFunctionality method', () => {
component.changeFunctionalityStatus(fControl, 'hello');
expect(component.removeFunctionality).toHaveBeenCalledWith(fControl);
});
it('should call addFunctionality method', () => {
returnVal.value = false;
component.changeFunctionalityStatus(fControl, 'hello');
expect(component.addFunctionality).toHaveBeenCalledWith(fControl);
});
});