【发布时间】:2021-07-01 13:34:50
【问题描述】:
在我的单元测试中,我有一个正在使用的模拟服务类MockWorkflowService,它有一个方法getActiveTask。大部分测试都需要此方法的默认功能,但是有几个测试我需要重写此方法并让它返回 null。但是我这样做没有任何运气(请参阅代码块底部的测试):
class MockWorkflowService {
getActiveTask() {
return new Action({
name: 'test',
value: 4
})
}
}
describe('MyComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
MyComponent
],
providers: [
TranslateService,
{ provide: WorkflowService, useClass: MockWorkflowService }
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should ...', () => {
// TRYING TO DO SOMETHING LIKE
MockWorkflowService.getActiveTask = () => null;
});
})
我尝试执行 MockWorkflowService.prototype.getActiveTask = () => null,它适用于我想要覆盖它的单个测试,但随后该方法对所有剩余测试返回 null。
任何帮助将不胜感激,谢谢。
【问题讨论】:
标签: angular unit-testing jasmine