2016-06-22 46 views
4

我學習NativeScript與Angular2,我寫了下面的測試一個簡單的組件:單元測試:無提供DirectiveResolver

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! 
    [email protected]:///app/tns_modules/@angular/core/src/facade/exceptions.js:17:32 
    [email protected]:///app/tns_modules/@angular/core/src/di/reflective_exceptions.js:39:20 
    [email protected]:///app/tns_modules/@angular/core/src/di/reflective_exceptions.js:75:20 
    [email protected]:///app/tns_modules/@angular/core/src/di/reflective_injector.js:776:62 
    [email protected]:///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 
    [email protected]:///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 
    [email protected]:///app/tns_modules/timer/timer.js:17:26 
    [email protected][native code] 
    [email protected]:///app/tns_modules/application/application.js:233:26 
    [email protected]:///app/./tns_modules/nativescript-unit-test-runner/app.js:3:18 
    [email protected][native code] 
    [email protected][native code] 
    [native code] 
    [email protected][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"]); 
     } 
    } 
} 

ç任何人都會提供關於如何解決此錯誤的線索並繼續進行我的測試? 在此先感謝。

回答

0

你試過

beforeEachProviders(() => [TestComponentBuilder, provide(UserService, { useClass: userMockService })]); 

,而不是

beforeEachProviders(() => [TestComponentBuilder]); 
    beforeEachProviders(() => [provide(UserService, { useClass: userMockService })]); 
+0

是,相同的結果 –

相關問題