gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:09:39 29 4
gpt4 key购买 nike

我在 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 上进行一些单元测试。

关于unit-testing - NativeScript Angular2 应用程序中的测试组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43925971/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com