如何使用TestBed和Jasmine在NativeScript中实现单元测试?

Geo*_*e S 8 testbed karma-jasmine nativescript angular

我正在设置一个NativeScript-Angular项目,并想使用Jasmine-Karma实现单元测试,以便使用css选择器测试我的组件。如何为一个简单的组件设置一个简单的单元测试(除了官方存储库中提供的样本测试之外)?

这是针对使用带有Android API级别28的NativeScript CLI 6.0的新项目的。

我已经尝试使用常规的Angular TestBed,据称此博客文章支持它:https ://www.nativescript.org/blog/announcing-the-nativescript-4.1-release

我还尝试在官方nativescript-angular存储库上遵循他们的工作测试:https : //github.com/NativeScript/nativescript-angular/tree/master/tests

无论哪种方式,在尝试执行自己的实现时,我似乎都在做错事,因为出现以下错误:
Uncaught Error: Zone already loaded
TypeError: Cannot read property 'injector' of null
TypeError: Cannot read property 'getComponentFromError' of null
TypeError: Cannot read property 'currentPage' of undefined

有没有人设法让Jasmine-Karma在NativeScript中使用TestBed单元测试?

test-main.ts

import "nativescript-angular/zone-js/testing.jasmine";
import { nsTestBedInit } from "nativescript-angular/testing";
nsTestBedInit();
Run Code Online (Sandbox Code Playgroud)

example.ts

import { ItemsComponent } from '~/app/item/items.component';
import { By } from '@angular/platform-browser';
import { nsTestBedBeforeEach, nsTestBedAfterEach, nsTestBedRender } from 'nativescript-angular/testing';

describe('item-detail-component', () => {
  beforeEach(nsTestBedBeforeEach(
    [ItemsComponent]
  ));
  afterEach(nsTestBedAfterEach());

  it(`should contain items`, () => {
    return nsTestBedRender(ItemsComponent).then((fixture) => {
      fixture.detectChanges();
      const list = fixture.debugElement.query(By.css('.list-group'));

      expect(list).toBeDefined();
    });
  })
});
Run Code Online (Sandbox Code Playgroud)

我希望能够运行测试而不会出现任何错误。

我为每个测试实现都包括了两个存储库。
重现步骤:
1.下载仓库
2. yarn install
3。tns test android

https://github.com/gsavchenko/nativescript-ns-testbed

更新

对于其他想知道如何使用端到端测试进一步测试前端的人,appium似乎是https://docs.nativescript.org/plugins/ui-tests

hyp*_*y2k 3

要使用 TestBed,您必须将其更改karma.conf.js为:

    // list of files / patterns to load in the browser
    files: [
      'src/tests/setup.ts',
      'src/tests/**/*.spec.ts'
    ],

Run Code Online (Sandbox Code Playgroud)

对于 jasmine ,该文件src/tests/setup.ts应如下所示:

import "nativescript-angular/zone-js/testing.jasmine";
import {nsTestBedInit} from "nativescript-angular/testing";
nsTestBedInit();

Run Code Online (Sandbox Code Playgroud)

或者如果使用摩卡:

import "nativescript-angular/zone-js/testing.mocha";
import {nsTestBedInit} from "nativescript-angular/testing";
nsTestBedInit();

Run Code Online (Sandbox Code Playgroud)

您可以在这里找到示例:https ://github.com/hypery2k/tns_testbed_sample