无法加载 TestbedHarnessEnvironment 或如何单击 MatRadioButton
Can't load TestbedHarnessEnvironment or how to click on MatRadioButton
在我的 Angular 10 单元测试应用程序中,我需要单击 MatRadioButton。
在这里 https://material.angular.io/guide/using-component-harnesses 我找到了如何操作的说明,但是当我添加这一行时:
loader = TestbedHarnessEnvironment.loader(fixture);
我开始收到错误消息:
TypeError: Cannot read property 'nativeElement' of undefined
at
at Function.loader (http://localhost:9876/karma_webpack/node_modules/@angular/cdk/fesm2015/testing/testbed.js:589:1)
at http://localhost:9876/karma_webpack/src/app/maps/maps.component.spec.ts:55:40
at
at http://localhost:9876/karma_webpack/node_modules/tslib/tslib.es6.js:74:1
at new ZoneAwarePromise (http://localhost:9876/karma_webpack/node_modules/zone.js/dist/zone-evergreen.js:960:1)
at __awaiter (http://localhost:9876/karma_webpack/node_modules/tslib/tslib.es6.js:70:1)
at UserContext. (http://localhost:9876/karma_webpack/src/app/maps/maps.component.spec.ts:48:26)
at ZoneDelegate.invoke (http://localhost:9876/karma_webpack/node_modules/zone.js/dist/zone-evergreen.js:364:1)
at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke
(http://localhost:9876/karma_webpack/node_modules/zone.js/dist/zone-testing.js:292:1)
at ZoneDelegate.invoke (http://localhost:9876/karma_webpack/node_modules/zone.js/dist/zone-evergreen.js:363:1)
谷歌搜索除了指出一些将要修复的错误外没有任何帮助 (https://www.gitmemory.com/issue/angular/components/21632/764975917)。
如何解决?或者如何点击 MatRadioButton?我是否应该使用原生元素通过 DOM 模拟点击?
https://www.gitmemory.com/issue/angular/components/21632/764975917有一个很好的答案:
The unit tests fail when the harness loader is created in an async
beforeEach and the TS target is ES2018. StackBlitz Demo
There are three solutions to this problem.
- Move the call to TestbedHarnessEnvironment.loader to the it function (demo).
- Use waitForAsync in the beforeEach instead of an async function (demo).
- Change the target to ES2016 (demo).
不过,要使用其建议之一(使用 waitForAsync 而不是异步)需要将 Angular 升级到 v11。
在我的 Angular 10 单元测试应用程序中,我需要单击 MatRadioButton。
在这里 https://material.angular.io/guide/using-component-harnesses 我找到了如何操作的说明,但是当我添加这一行时:
loader = TestbedHarnessEnvironment.loader(fixture);
我开始收到错误消息:
TypeError: Cannot read property 'nativeElement' of undefined at at Function.loader (http://localhost:9876/karma_webpack/node_modules/@angular/cdk/fesm2015/testing/testbed.js:589:1) at http://localhost:9876/karma_webpack/src/app/maps/maps.component.spec.ts:55:40 at at http://localhost:9876/karma_webpack/node_modules/tslib/tslib.es6.js:74:1 at new ZoneAwarePromise (http://localhost:9876/karma_webpack/node_modules/zone.js/dist/zone-evergreen.js:960:1) at __awaiter (http://localhost:9876/karma_webpack/node_modules/tslib/tslib.es6.js:70:1) at UserContext. (http://localhost:9876/karma_webpack/src/app/maps/maps.component.spec.ts:48:26) at ZoneDelegate.invoke (http://localhost:9876/karma_webpack/node_modules/zone.js/dist/zone-evergreen.js:364:1) at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/karma_webpack/node_modules/zone.js/dist/zone-testing.js:292:1) at ZoneDelegate.invoke (http://localhost:9876/karma_webpack/node_modules/zone.js/dist/zone-evergreen.js:363:1)
谷歌搜索除了指出一些将要修复的错误外没有任何帮助 (https://www.gitmemory.com/issue/angular/components/21632/764975917)。
如何解决?或者如何点击 MatRadioButton?我是否应该使用原生元素通过 DOM 模拟点击?
https://www.gitmemory.com/issue/angular/components/21632/764975917有一个很好的答案:
The unit tests fail when the harness loader is created in an async beforeEach and the TS target is ES2018. StackBlitz Demo
There are three solutions to this problem.
- Move the call to TestbedHarnessEnvironment.loader to the it function (demo).
- Use waitForAsync in the beforeEach instead of an async function (demo).
- Change the target to ES2016 (demo).
不过,要使用其建议之一(使用 waitForAsync 而不是异步)需要将 Angular 升级到 v11。