我们如何将 TestBed 用于带有 NativeScript 的组件?
How we use TestBed for components with NativeScript?
我正在使用 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?如果是这样,你能提供一个如何做到这一点的例子吗?
你应该return承诺,
it("should be defined", () => {
return nsTestBedRender(ItemsComponent).then(fixture => {
expect(fixture.componentInstance).toBeTruthy();
});
});
我正在使用 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?如果是这样,你能提供一个如何做到这一点的例子吗?
你应该return承诺,
it("should be defined", () => {
return nsTestBedRender(ItemsComponent).then(fixture => {
expect(fixture.componentInstance).toBeTruthy();
});
});