问题似乎出在app.component.spec.ts文件上.请参阅下面的错误消息: AppComponentTest > -> should render title in a h1 tagFailed: Cannot read property 'textContent' of null at <Jasmine> at UserContext.eval (./src/app/app.component.spec.ts?:31:44) at ZoneDelegate.invoke (./node_modules/zone.js/dist/zone.js?:387:26) at AsyncTestZoneSpec.onInvoke (./node_modules/zone.js/dist/zone-testing.js?:712:39) at ProxyZoneSpec.onInvoke (./node_modules/zone.js/dist/zone-testing.js?:284:39) at ZoneDelegate.invoke (./node_modules/zone.js/dist/zone.js?:386:32) at Zone.runGuarded (./node_modules/zone.js/dist/zone.js?:150:47) at runInTestZone (./node_modules/zone.js/dist/zone-testing.js?:840:29) at UserContext.eval (./node_modules/zone.js/dist/zone-testing.js?:774:17) at ZoneDelegate.invoke (./node_modules/zone.js/dist/zone.js?:387:26) at ProxyZoneSpec.onInvoke (./node_modules/zone.js/dist/zone-testing.js?:287:39) 解决方案被测组件的设置包括CUSTOM_ELEMENTS_SCHEMA,它允许在模板中使用自定义标签元素,而无需通过实例化关联的组件来使测试复杂化.结果,从父级测试子级组件中的元素失败.解决方案是将失败的测试规范移到适当的子组件中.I have one failure when I run 'ng test' on my Angular 6 app:Please see sample app... SampleAppThe problem appears to be with the app.component.spec.ts file. See error message below:AppComponentTest > -> should render title in a h1 tagFailed: Cannot read property 'textContent' of null at <Jasmine> at UserContext.eval (./src/app/app.component.spec.ts?:31:44) at ZoneDelegate.invoke (./node_modules/zone.js/dist/zone.js?:387:26) at AsyncTestZoneSpec.onInvoke (./node_modules/zone.js/dist/zone-testing.js?:712:39) at ProxyZoneSpec.onInvoke (./node_modules/zone.js/dist/zone-testing.js?:284:39) at ZoneDelegate.invoke (./node_modules/zone.js/dist/zone.js?:386:32) at Zone.runGuarded (./node_modules/zone.js/dist/zone.js?:150:47) at runInTestZone (./node_modules/zone.js/dist/zone-testing.js?:840:29) at UserContext.eval (./node_modules/zone.js/dist/zone-testing.js?:774:17) at ZoneDelegate.invoke (./node_modules/zone.js/dist/zone.js?:387:26) at ProxyZoneSpec.onInvoke (./node_modules/zone.js/dist/zone-testing.js?:287:39) 解决方案 The setup for the component under test includes CUSTOM_ELEMENTS_SCHEMA, which allows the use of custom tag elements in the template without complicating the test by instantiating the associated component. As a result, testing for an element in a child component from the parent fails. The solution is to move the failing test spec into proper child component. 这篇关于Angular-Karma测试-失败:无法读取null的属性'textContent'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-14 20:48