【发布时间】:2018-01-06 00:48:31
【问题描述】:
我正在尝试测试我的 Angular 4.1.0 组件 -
export class CellComponent implements OnInit {
lines: Observable<Array<ILine>>;
@Input() dep: string;
@Input() embedded: boolean;
@Input() dashboard: boolean;
constructor(
public dataService: CellService,
private route: ActivatedRoute,
private router: Router, private store: Store<AppStore>) {
}
}
然而,一个简单的“应该创建”测试会抛出这个神秘的错误......
NetworkError:无法在“XMLHttpRequest”上执行“发送”:无法加载“ng:///DynamicTestModule/module.ngfactory.js”。
所以我发现了this 问题,这表明问题在于组件具有未设置的@Input)_ 参数,但是,如果我像这样修改我的测试:
it('should create', inject([CellComponent], (cmp: CellComponent) => {
cmp.dep = '';
cmp.embedded = false;
cmp.dashboard = false;
expect(cmp).toBeTruthy();
}));
然后我仍然遇到同样的问题,同样,如果我从组件中删除 @Input() 注释,仍然没有区别。我怎样才能让这些测试通过?
【问题讨论】:
-
为了创建组件,您需要提供所有依赖项。你能展示你所有的测试设置吗?我将尝试在plnkr 上重现该问题
-
我遇到了同样的问题,并找到了与您相同的帖子。我能够找到解决方案。我最终发布了另一个问题,但您可以在这里查看:stackoverflow.com/a/45419372/6739517 希望对您有所帮助!
标签: angular unit-testing karma-jasmine angular-cli