【发布时间】:2021-11-05 16:15:27
【问题描述】:
我正在为组件编写单元测试。它的一种方法使用服务,它给了我一个cannot read property 'then' of undefined 错误。
我已经看到如何在测试中调用服务,但我正在尝试使用一种方法来调用我的规范文件之外的服务。
规格
describe('MyComponent', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
schemas: [CUSTOM_ELEMENTS_SCHEMA],
imports: [...],
declarations: [MyComponent],
providers: [...],
}).compileComponents();
})
);
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should save', () => {
...
component.myMethod();
});
});
组件
public myMethod() {
...
this.myService.something(this.item).then((resp) => {
// Error comes from here
});
}
}
【问题讨论】: