Angular-Karma测试-失败:无法读取null的属性'textContent' [英] Angular - Karma Testing - Failed: Cannot read property 'textContent' of null

查看:52
本文介绍了Angular-Karma测试-失败:无法读取null的属性'textContent'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular 6应用上运行"ng测试"时,我遇到了一个失败:

失败:无法读取null的属性'textContent'


请参阅示例应用程序... SampleApp


问题似乎出在app.component.spec.ts文件上.请参阅下面的错误消息:

 AppComponentTest > -> should render title in a h1 tag
Failed: 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:

Failed: Cannot read property 'textContent' of null


Please see sample app... SampleApp


The problem appears to be with the app.component.spec.ts file. See error message below:

AppComponentTest > -> should render title in a h1 tag
Failed: 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'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆