我有angular 2网页包应用程序,所有网页包,按照angular创建的karma配置.io网页包指南.

我try 在异步挡路中加入代码. 然后我得到以下错误. 错误:无法对‘XMLHttpRequest’执行‘Send’:无法加载‘ng:/DynamicTestModule​/module.ngfactory.js’


不带异步的代码

beforeeach(()=> {
TestBed.configureTestingModule({
imports:[],
declaration :[Mycomp],
providers:[{ provide:MyService, useclass:MyMockService}]
});
 fixture=TestBed.createComponent(Mycomp);
 console.log(' before detect changes'):
 fixture.detectChanges():
 console.log('after detect changes');// this is not getting   
    logged .. karma shows 0 of 1 executed successfully

 });

使用异步

  beforeeach(async(()=> {
 TestBed.configureTestingModule({
  imports:[],
  declaration :[Mycomp],
  providers:[{ provide:MyService,       useclass:MyMockService}]
  });
   fixture=TestBed.createComponent(Mycomp);
    fixture.detectChanges():
  }));

获取错误无法加载dynamictestmodule/模块.工厂.js

推荐答案

昨天我自己也遇到了这个问题.问题是,我的组件类上有一个Input()属性,而我在测试中没有设置该属性.例如,在我的组件中.ts:

@Component({
  selector: 'my-component'
})
export class MyComponent {
  @Input() title: string;
}

还有我的零件.规格:

beforeEach(() => {
  fixture = TestBed.createComponent(MyComponent);
  component = fixture.componentInstance;
  component.title = 'Hello there!' // <-- this is required!
  fixture.detectChanges();
});

或者,您可以在组件中的某个位置提供默认值.无论哪种方式,如果没有设置任何输入,测试都会崩溃,并且您会得到不直观的错误.

注意:运行ng test -sm=false将给出导致问题的实际错误消息.学分:https://stackoverflow.com/a/45802115/61311

Angular相关问答推荐

RXJS运算符用于combineLatest,不含空值

Angular 升级到v16和Ant设计错误:';nzComponentParams';类型';ModalOptions';中不存在

如何包装NGB carousel ?

当嵌套在异步容器中时,S会阻止具有动态值的ionic 段工作吗?

如何在 Angular 中每 x 秒从 REST api 加载数据并更新 UI 而不会闪烁?

当信号的值发生变化时,模板不会更新

Angular 信号 - 使用 mutate() 与使用 forEach() react 性

Router.navigate() 在功能拦截器内不起作用

Angular Mat Select 显示键盘焦点和鼠标悬停焦点的问题

Angular:为什么在我的自定义 ErrorHandler 中订阅主题不触发?

包含与 ngFor 相关内容的 HTML 输入

如何以Angular 改变环境

刷新页面导致 401 错误-Angular 8

在 Angular material表上调用 renderRows()

清除Angular2中的输入文本字段

Angular 2.0 中 $scope 的替代品

angular2:错误: TypeError: Cannot read property '...' of undefined

如何将组件导入Angular 2中的另一个根组件

Angular 2 组件不是任何 NgModule 的一部分

在 Angular 2 中对 observable 进行单元测试