Harness 正在尝试使用已被破坏的固定装置

Buc*_*ket 5 javascript jasmine typescript kendo-grid karma-jasmine

我有一个简单的单元测试,它通过了,但在运行时抛出错误“Harness 正在尝试使用已被破坏的固定装置” ng test --watch=false。发生这种情况是因为我将组件放入吗<ng-template>?有谁知道如何解决这个错误?

菜单组件规格

describe("MenuComponent", () => {
    let testHostFixture: ComponentFixture<TestHostComponent>;
    let myMenuHarness: MenuHarness;

    beforeEach(
        waitForAsync(() => {
            TestBed.configureTestingModule({
                declarations: [...],
                imports: [...],
                providers: [...],
            }).compileComponents();
        })
    );

    beforeEach(async () => {
        testHostFixture = TestBed.createComponent(TestHostComponent);
        myMenuHarness = await TestbedHarnessEnvironment.harnessForFixture(
            testHostFixture,
            MenuHarness
        );
        testHostFixture.detectChanges();
    });

    describe("simple test", () => {
        it("should pass", async () => {
            await myMenuHarness.openMenu();
            expect(true);
        });
    });

    @Component({
        selector: `my-test-host-component`,
        template: `<kendo-grid ...>
            <ng-template kendoGridColumnMenuTemplate let-service="service">
                <app-my-menu [service]="service"></app-my-menu>
            </ng-template>
            <kendo-grid-column ... ></kendo-grid-column>
        </kendo-grid>`,
    })
    class TestHostComponent {
        @ViewChild(MenuComponent)
        public menu?: MenuComponent;
    }
});
Run Code Online (Sandbox Code Playgroud)

菜单安全带

public async openColumnMenu() {
    const element = await this.queryColumnMenuButton();
    element?.click();
}
Run Code Online (Sandbox Code Playgroud)

输出

Error: Harness is attempting to use a fixture that has already been destroyed.
    at http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/cdk/fesm2020/testing/testbed.mjs:720:23
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular-devkit/build-angular/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:3:1)
    at _next (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular-devkit/build-angular/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:25:1)     
    at http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular-devkit/build-angular/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:32:1
    at new ZoneAwarePromise (http://localhost:9877/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:1340:1)
    at http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular-devkit/build-angular/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:21:1
    at TestbedHarnessEnvironment.forceStabilize (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/cdk/fesm2020/testing/testbed.mjs:717:28)
    at UnitTestElement._stabilize (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/cdk/fesm2020/testing/testbed.mjs:751:56)
    at http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/cdk/fesm2020/testing/testbed.mjs:449:20, 'Error: Harness is attempting to use a fixture that has already been destroyed.
    at http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/cdk/fesm2020/testing/testbed.mjs:720:23
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular-devkit/build-angular/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:3:1)
    at _next (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular-devkit/build-angular/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:25:1)     
    at http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular-devkit/build-angular/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:32:1
    at new ZoneAwarePromise (http://localhost:9877/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:1340:1)
    at http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular-devkit/build-angular/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:21:1
    at TestbedHarnessEnvironment.forceStabilize (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/cdk/fesm2020/testing/testbed.mjs:717:28)
    at UnitTestElement._stabilize (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/cdk/fesm2020/testing/testbed.mjs:751:56)
    at http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/cdk/fesm2020/testing/testbed.mjs:449:20'
Run Code Online (Sandbox Code Playgroud)

小智 0

await element?.click();
Run Code Online (Sandbox Code Playgroud)

为我解决了问题