【问题标题】:How we use TestBed for components with NativeScript?我们如何将 TestBed 用于带有 NativeScript 的组件?
【发布时间】:2020-03-19 12:47:20
【问题描述】:

我正在使用 NativeScript 6+ 和 Angular 8+ 编写应用程序。我正在尝试编写一些单元测试并让它们启动并运行。我已阅读有关单元测试的文档:https://docs.nativescript.org/tooling/testing/testing 我已经启动了一些单元测试并使用普通组件运行。例如

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();
    });

});
});

我看到我们可以通过在构造函数中实例化组件和模拟服务来测试具有依赖关系的组件。 https://angular.io/guide/testing#service-tests

我尝试按照此网页上的教程进行操作,但始终出现错误:

示例代码和存储库:

https://github.com/aquinn637/Fresh

import { ItemsComponent } from "~/app/item/items.component";
import { ItemService } from "~/app/item/item.service";
import { nsTestBedBeforeEach, nsTestBedAfterEach, nsTestBedRender } from "nativescript-angular/testing";

describe('Renderer E2E', () => {

    beforeEach(nsTestBedBeforeEach([ItemsComponent], [ItemService]));
    afterEach(nsTestBedAfterEach(false));

    it('should be defined',  () => {

        nsTestBedRender(ItemsComponent).then((fixture) => {
            expect(fixture.componentInstance).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)

是否可以在 NativeScript 中为我们的组件使用 TestBed?如果是这样,你能提供一个如何做到这一点的例子吗?

【问题讨论】:

    标签: angular unit-testing nativescript karma-jasmine


    【解决方案1】:

    你应该兑现承诺,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-08
      • 1970-01-01
      • 2017-01-09
      • 2016-12-19
      • 2018-12-09
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      相关资源
      最近更新 更多