使用Testbed的本机脚本测试不起作用

cin*_*nni 5 unit-testing jasmine nativescript angular2-nativescript

对于该项目,我们当前正在使用带有angular2的本机脚本启动,单元测试对我们来说至关重要。因此,我首先尝试为给定的Groceries示例实现一些简单的操作,以使其更实用。非常简单的测试,例如测试电子邮件地址的验证是否正常,并且执行起来并不繁琐。一旦我开始进行更复杂的测试,就只有在涉及Testbed时才出现错误。

这是我在Testbed上执行的最简单的启动实现:

import "reflect-metadata";
import 'nativescript-angular/zone.js/dist/zone-nativescript';
import {TestBed} from '@angular/core/testing';
import { UserService } from "../../../shared/user/user.service";

declare var describe: any;
declare var expect: any;
declare var it: any;
declare var beforeEach: any;

describe('UserService', () => {
    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [UserService]
        });
    }); 

    it("just testing", () => {
        console.log('hier');
        expect(false).toBe(false);
    });
});
Run Code Online (Sandbox Code Playgroud)

这是我运行该错误时会收到的那种错误:

sampleGroceries[66839]: CONSOLE LOG file:///app/tests/shared/user/user.service.spec.js:13:20: hier
NativeScript / 10.0 (10.0; iPhone) A suite contains spec with an expectation FAILED
TypeError: undefined is not an object (evaluating 'ProxyZoneSpec.assertPresent') in file:///app/tns_modules/@angular/core/testing/../bundles/core-testing.umd.js (line 385)
resetFakeAsyncZone@file:///app/tns_modules/@angular/core/testing/../bundles/core-testing.umd.js:385:22
file:///app/tns_modules/@angular/core/testing/../bundles/core-testing.umd.js:1255:31
attemptSync
run
execute
queueRunnerFactory
execute
fn
attemptAsync
run
execute
queueRunnerFactory
fn
attemptAsync
run
execute
queueRunnerFactory
execute
execute
runTests@file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:216:23
file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:187:101
tick@file:///app/tns_modules/timer/timer.js:17:26
UIApplicationMain@[native code]
start@file:///app/tns_modules/application/application.js:234:26
anonymous@file:///app/./tns_modules/nativescript-unit-test-runner/app.js:3:18
evaluate@[native code]
moduleEvaluation@[native code]
[native code]
promiseReactionJob@[native code]
NativeScript / 10.0 (10.0; iPhone) UserService just testing FAILED
TypeError: undefined is not an object (evaluating 'ProxyZoneSpec.assertPresent') in file:///app/tns_modules/@angular/core/testing/../bundles/core-testing.umd.js (line 385)
resetFakeAsyncZone@file:///app/tns_modules/@angular/core/testing/../bundles/core-testing.umd.js:385:22
file:///app/tns_modules/@angular/core/testing/../bundles/core-testing.umd.js:1255:31
attemptSync
run
execute
queueRunnerFactory
execute
fn
attemptAsync
run
execute
queueRunnerFactory
fn
attemptAsync
run
onComplete
clearStack
run
complete
clearStack
run
execute
queueRunnerFactory
execute
fn
attemptAsync
run
execute
queueRunnerFactory
fn
attemptAsync
run
execute
queueRunnerFactory
execute
execute
runTests@file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:216:23
file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:187:101
tick@file:///app/tns_modules/timer/timer.js:17:26
UIApplicationMain@[native code]
start@file:///app/tns_modules/application/application.js:234:26
anonymous@file:///app/./tns_modules/nativescript-unit-test-runner/app.js:3:18
evaluate@[native code]
moduleEvaluation@[native code]
[native code]
promiseReactionJob@[native code]
NativeScript / 10.0 (10.0; iPhone): Executed 2 of 2 (2 FAILED) ERROR (0.02 secs / 0.002 secs)
Run Code Online (Sandbox Code Playgroud)

我尝试了各种操作,例如import zone.js / dist / proxy.js以及许多其他没有成功的东西。

有人知道我在想什么吗?您如何正确实现它,以便单元测试将适用于带有测试平台的本机脚本???

use*_*710 0

要将 TestBed 与 NativeScript 一起使用,您必须使用 TestBed 的 NativeScript 版本。这是因为 NativeScript 没有在浏览器中运行;它的视图是原生的,我们不处理 html。

这里有一些文档:https ://docs.nativescript.org/tooling/testing/testing

这里: https: //docs.nativescript.org/angular/tooling/testing/testing#testbed-integration

一旦集成了 NativeScript TestBed,大多数功能都类似于 Angular 的 TestBed 实现,例如 componentFixture