[我正在使用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用于我们的组件?如果是这样,您能提供一个示例吗?
您应该兑现承诺,
it("should be defined", () => {
return nsTestBedRender(ItemsComponent).then(fixture => {
expect(fixture.componentInstance).toBeTruthy();
});
});