问题描述
在我们的应用程序中,我们利用专门的服务来展示小吃,以便在必要时能够重新打开关闭的小吃。为其中一项服务功能编写单元测试时,出现以下错误:
No component factory found for GsSnackComponent. Did you add it to @NgModule.entryComponents?
GsSnackComponent
未定义为 EntryComponent
中的 SharedModule
,因为它在我们的 Angular 版本中不是必需的(项目从版本 9 开始,最近更新为12)。我认为在测试配置中声明它应该就足够了。但显然不是。我们结合使用 Jest 和 spectator,失败的单元测试如下所示。
import { MockComponent } from 'ng-mocks';
describe('SnackService',() => {
let spectator: spectatorService<SnackService>;
const createService = createServiceFactory<SnackService>({
service: SnackService,declarations: [MockComponent(GsSnackComponent)],entryComponents: [GsSnackComponent],});
beforeEach(() => {
spectator = createService();
});
test('should push timed snack',() => {
const snackId = spectator.service.pushTimedSnack('mockMessage',1); // Fails
// ...
});
});
我们要测试的功能:
// test for this function fails. Since no custom Snack Component is used,// the default GsSnackComponent is passed to pushSnackComponent()
pushTimedSnack(message: string,id?: number): number {
return this.pushSnackComponent(GsSnackComponent,{ message,hasTimeout: true },id);
}
// calls this function
pushSnackComponent(component: ComponentType<any>,data?: AnyGsSnackProperties,id?: number): number {
// ...
return snackId;
}
知道还能做什么吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)