对于我们目前使用带有 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);
});
});
这是我一运行就得到的那种错误:
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)
我尝试了各种各样的东西,比如 import zone.js/dist/proxy.js 和很多其他的东西,但都没有成功。
有人知道我缺少什么吗?您如何正确实现它,以便单元测试适用于带有测试床的 native 脚本???
最佳答案
要将 TestBed 与 NativeScript 一起使用,您必须使用 TestBed 的 NativeScript 版本。这是因为 NativeScript 没有在浏览器中运行;它的 View 是原生的,我们不处理 html。
这里有一些文档:https://docs.nativescript.org/tooling/testing/testing
在这里:https://docs.nativescript.org/angular/tooling/testing/testing#testbed-integration
一旦您集成了 NativeScript TestBed,大部分功能类似于 Angular 的 TestBed 实现,例如组件夹具
关于unit-testing - 使用 Testbed 进行 Nativescript 测试不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40263631/