Angular 我们如何将测试床用于具有NativeScript的组件?

Angular 我们如何将测试床用于具有NativeScript的组件?,angular,unit-testing,nativescript,karma-jasmine,Angular,Unit Testing,Nativescript,Karma Jasmine,我正在用NativeScript 6+和Angular 8+编写一个应用程序。我正在尝试编写一些单元测试,并让它们开始运行。我已经阅读了关于单元测试的文档:我已经用普通组件进行了一些单元测试。e、 g import { ItemsComponent } from "~/app/item/items.component"; import { ItemService } from "~/app/item/item.service"; describe('Items Component Standa

我正在用NativeScript 6+和Angular 8+编写一个应用程序。我正在尝试编写一些单元测试,并让它们开始运行。我已经阅读了关于单元测试的文档:我已经用普通组件进行了一些单元测试。e、 g

import { ItemsComponent } from "~/app/item/items.component";
import { ItemService } from "~/app/item/item.service";

describe('Items Component Standard Test', () => {

    let  component: ItemsComponent;

    beforeEach(() => {
        component = new ItemsComponent(new ItemService());
    });

    it('should be defined', () => {
        expect(component).toBeTruthy();
    });

});
});
我看到,我们可以通过在构造函数中实例化依赖性组件以及使用模拟服务来测试这些组件

我试图按照此网页上的教程进行操作,但不断出现错误:

示例代码和存储库:

错误:

CONSOLE WARN file:///node_modules/@nativescript/angular/testing/src/util.js:171:0: nsTestBedRender called without nsTestBedBeforeEach/nsTestBedAfter each. You are responsible for calling 'fixture.destroy()' when your test is done in order to clean up the components that are created.

CONSOLE ERROR file:///app/vendor.js:85281:30: Unhandled Promise rejection: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out ; Zone: ProxyZone ; Task: Promise.then ; Value: Error: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out ZoneAwareError(file:///node_modules/@nativescript/angular/zone-js/dist/zone-nativescript.js:1298:0)

at

at expect

at

at file:///node_modules/@nativescript/angular/zone-js/dist/zone-nativescript.js:138:0

at file:///node_modules/@nativescript/angular/zone-js/dist/zone-nativescript.js:883:0

at file:///node_modules/@nativescript/angular/zone-js/dist/zone-nativescript.js:188:0

at drainMicroTaskQueue(file:///node_modules/@nativescript/angular/zone-js/dist/zone-nativescript.js:595:0)

at file:///node_modules/@nativescript/angular/zone-js/dist/zone-nativescript.js:500:0

at timer(file:///node_modules/@nativescript/angular/zone-js/dist/zone-nativescript.js:1561:0)

at invoke(file:///node_modules/@nativescript/core/timer/timer.js:54:30)

at file:///node_modules/@nativescript/angular/zone-js/dist/zone-nativescript.js:151:0

at file:///node_modules/@nativescript/core/timer/timer.js:18:0

at UIApplicationMain([native code])

at run(file:///node_modules/@nativescript/core/application/application.js:312:0)

at ../node_modules/nativescript-unit-test-runner/app/bundle-app.js(file:///node_modules/nativescript-unit-test-runner/app/bundle-app.js:3:0)

at __webpack_require__(file:///src/webpack/bootstrap:74:0)

at file:///src/main.ts:11:16

at ./main.ts<…>

19 03 2020 12:01:11.660:WARN [NativeScript / 13.3 (13.3; iPad)]: Disconnected (0NativeScript / 13.3 (13.3; iPad) ERROR

  DisconnectedClient disconnected from CONNECTED state (transport error)

NativeScript / 13.3 (13.3; iPad): Executed 4 of 4 SUCCESS (0.161 secs / 0.099 secs)
控制台警告file:///node_modules/@nativescript/angular/testing/src/util.js:171:0:nsTestBedRender调用时不带NSTestBedBeforeach/NSTestBedAftereach。您负责在测试完成时调用“fixture.destroy()”,以清理创建的组件。
控制台错误file:///app/vendor.js:85281:30: 未处理的承诺拒绝:在没有当前规范时使用“expect”,这可能是因为异步测试超时;区域:ProxyZone;任务:承诺;值:错误:在没有当前规范时使用了“expect”,这可能是因为异步测试超时了ZoneAwareError(file:///node_modules/@nativescript/angular/zone js/dist/zone nativescript.js:1298:0)
在
意料之中
在
在file:///node_modules/@nativescript/angular/zone js/dist/zone nativescript.js:138:0
在file:///node_modules/@nativescript/angular/zone js/dist/zone nativescript.js:883:0
在file:///node_modules/@nativescript/angular/zone js/dist/zone nativescript.js:188:0
在drainMicroTaskQueue(file:///node_modules/@nativescript/angular/zone js/dist/zone nativescript.js:595:0)
在file:///node_modules/@nativescript/angular/zone js/dist/zone nativescript.js:500:0
准时(file:///node_modules/@nativescript/angular/zone js/dist/zone nativescript.js:1561:0)
在调用时(file:///node_modules/@nativescript/core/timer/timer.js:54:30)
在file:///node_modules/@nativescript/angular/zone js/dist/zone nativescript.js:151:0
在file:///node_modules/@nativescript/core/timer/timer.js:18:0
在UIApplicationMain([本机代码])
在逃(file:///node_modules/@nativescript/core/application/application.js:312:0)
位于../node_modules/nativescript单元测试运行程序/app/bundle-app.js(file:///node_modules/nativescript-unit-test-runner/app/bundle-app.js:3:0)
在需要的网页上__(file:///src/webpack/bootstrap:74:0)
在file:///src/main.ts:11:16
at./main.ts
19 03 2020 12:01:11.660:警告[NativeScript/13.3(13.3;iPad)]:断开连接(0NativeScript/13.3(13.3;iPad)错误
DisconnectedClient已从连接状态断开连接(传输错误)
NativeScript/13.3(13.3;iPad):成功执行4次(0.161秒/0.099秒)

是否可以在NativeScript中为我们的组件使用TestBed?如果可以,您能否提供一个如何实现这一点的示例?

您应该返回承诺

it("should be defined", () => {
    return nsTestBedRender(ItemsComponent).then(fixture => {
        expect(fixture.componentInstance).toBeTruthy();
    });
});
it("should be defined", () => {
    return nsTestBedRender(ItemsComponent).then(fixture => {
        expect(fixture.componentInstance).toBeTruthy();
    });
});