NativeScript Angular2 应用程序中的测试组件

Testing component in NativeScript Angular2 app

我在 NativeScript Angular 2 应用程序中进行非常简单的组件测试时遇到问题。我似乎不能只调用 "new Component()" 然后像 this 测试示例中那样测试它。我正在尝试实施 Angular 2 TestBed 以获得对该组件的访问权限。这似乎也不起作用。有没有人 运行 遇到过类似的问题或对此有任何经验?

被测组件:

import { Component } from "@angular/core";
@Component({
    selector: "links",
    templateUrl: "./components/links/links.component.html"
})

export class LinksComponent {
    test = "test";

    public constructor() {

    }
}

测试:

import "reflect-metadata";
import { LinksComponent } from "../../components/links/links.component";
import { ComponentFixture, TestBed } from '@angular/core/testing';

describe("Links Component", () => {

    let comp: LinksComponent;
    let fixture: ComponentFixture<LinksComponent>;

    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [ LinksComponent ],
        });

        fixture = TestBed.createComponent(LinksComponent);

        comp = fixture.componentInstance;
    });

    it("will pass because this is a silly test", () => {
        expect(comp.test).toEqual("test");
    })
});

日志输出:

NativeScript / 10.3 (10.3; iPhone)  ../../tests/components/links.spec.js at line 0 FAILED
ReferenceError: Can't find variable: Zone
NativeScript / 10.3 (10.3; iPhone): Executed 1 of 0 (1 FAILED) (0 secs / 0 seNativeScript / 10.3 (10.3; iPhone): Executed 1 of 0 (1 FAILED) ERROR (0.008 secs / 0 secs)
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:258:24: NSUTR: completeAck
May 11 16:16:43 --- last message repeated 1 time ---
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:90:28: NSUTR-socket.io: io server disconnect
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:151:24: NSUTR: disregarding second execution
Test run failed.

AFAIK TestBed 不适用于 NativeScript Angular。 要模拟组件,您需要使用 new 实例化它们,解析每个依赖项。

你应该学习这个 repository 以便能够在 NativeScript 上做一些单元测试。