【发布时间】:2022-11-04 16:56:52
【问题描述】:
在我的 Angular 应用程序中,我有一个 service which is provided at a component level:
@Component({
selector: 'my-component',
templateUrl: './my.component.html',
providers: [MyService],
})
export class MyComponent implements OnInit, OnDestroy { ... }
我在我的 Angular 测试中使用spectator,我正在尝试以下列方式测试提供服务的组件:
const createComponent = createComponentFactory({
component: MyComponent,
providers: [
// ... other services ...
mockProvider(MyService),
],
// ...
});
// ...
const spectator = createComponent();
const myService = spectator.inject(MyService);
但是,每当我尝试在myService 上模拟东西时,它都不起作用:似乎它正在获取MyService 的全局实例,而不是MyService 的组件级实例。
【问题讨论】:
标签: angular typescript angular-test angular-unit-test angular-spectator