我正在使用 Angular2 学习 NativeScript,并且我为一个简单的组件编写了以下测试:
import 'nativescript-angular/application';
import 'reflect-metadata';
import {LoginComponent} from '../pages/login/login.component';
import {UserMockService} from './mocks/userMock.service';
import {UserService} from '../shared/user/user.service';
import {inject, beforeEachProviders, it, describe, expect, beforeEach, async} from '@angular/core/testing';
import { TestComponentBuilder } from '@angular/compiler/testing';
import {provide} from '@angular/core';
describe('Login Component', () => {
let userMockService;
beforeEachProviders(() => [TestComponentBuilder]);
beforeEachProviders(() => [provide(UserService, { useClass: userMockService })]);
it('Should perform the login', inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
tcb.createAsync(LoginComponent).then(fixture => {
let component = fixture.nativeElement;
console.log(component);
expect(true).toBe(true);
});
}));
});
但是,当我运行 Nativescript 测试运行程序时,出现以下错误:
Error: No provider for DirectiveResolver!
BaseException@file:///app/tns_modules/@angular/core/src/facade/exceptions.js:17:32
AbstractProviderError@file:///app/tns_modules/@angular/core/src/di/reflective_exceptions.js:39:20
NoProviderError@file:///app/tns_modules/@angular/core/src/di/reflective_exceptions.js:75:20
_throwOrNull@file:///app/tns_modules/@angular/core/src/di/reflective_injector.js:776:62
initComponent@file:///app/tns_modules/@angular/compiler/testing/test_component_builder.js:296:60
file:///app/tests/login.component.test.js:14:24
file:///app/tns_modules/@angular/core/testing/testing.js:98:28
attemptAsync
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:233: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]
这是我要测试的组件:
import {Component, OnInit} from "@angular/core";
import {Router} from "@angular/router-deprecated";
import {Page} from 'ui/page'
import {User} from "../../shared/user/user";
import {UserService} from "../../shared/user/user.service";
@Component({
selector: "login",
providers: [UserService],
templateUrl: "pages/login/login.html",
styleUrls: ["pages/login/login-common.css"]
})
export class LoginComponent implements OnInit {
userName: string;
userPassword: string;
constructor(private userService: UserService,
private router: Router,
private page: Page) {
}
login() {
let user = new User(this.userName, this.userPassword);
this.userService.login(user)
.subscribe(
data => this.router.navigate(['Home']),
error => alert("Error on login. Check your credentials.")
);
}
ngOnInit() {
this.page.actionBarHidden = true;
if (this.userService.isLogged()) {
this.router.navigate(["Home"]);
}
}
}
谁能提供有关如何解决此错误并继续我的测试的线索?提前致谢。