对于我们目前正在使用带有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以及许多其他没有成功的东西。
有人知道我错过了什么吗?如何正确实现它,以便单元测试适用于带有testbed的本机脚本???
答案 0 :(得分: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