我在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.