2016-07-08 2 views
2

Это сбивает с толку, поскольку я не могу найти ничего связанного в документах Angular 2.0 и Router - устарел (да, я должен использовать его еще в своем проекте).Как тестировать устройство с угловым 2.0, которое включает версию, не поддерживающую маршрутизатор?

Моя служба выглядит следующим образом:

import { Injectable } from '@angular/core'; 
import { Http, Headers } from '@angular/http'; 
import { AuthHttp , JwtHelper } from 'angular2-jwt'; 
import { Router } from '@angular/router-deprecated'; 
import { UMS } from '../common/index'; 

@Injectable() 
export class UserService { 

    constructor(
    private router: Router, 
    private authHttp: AuthHttp, 
    private http: Http) { 

     this.router = router; 
     this.authHttp = authHttp; 
     this.http = http; 
    } 

    login(v) { 
     this.http.post(myUrl) 
     .subscribe(
     data => this.loginSuccess(data), 
     err => this.loginFailure(err) 
    ); 
    } 

} 

И мой тест, как это (на самом деле не волнует «это» часть на данный момент):

import { Http } from '@angular/http'; 
import { AuthHttp, JwtHelper } from 'angular2-jwt'; 
import { Router } from '@angular/router-deprecated'; 
import { 
    beforeEach, beforeEachProviders, 
    describe, xdescribe, 
    expect, it, xit, 
    async, inject 
} from '@angular/core/testing'; 
import { UserService } from './user.service'; 

describe('User Service',() => { 

    let service; 

    beforeEachProviders(() => [ 
    Router, 
    AuthHttp, 
    Http, 
    UserService 
    ]); 

    beforeEach(inject([ 
     Router, 
     AuthHttp, 
     Http, 
     UserService], s => { 
    service = s; 
    })); 

    it('Should have a login method',() => { 
     expect(service.login()).toBeTruthy(); 
    }); 

}); 

Когда я запускаю тест Я получаю эту ошибку: (кстати, я использую угловой-CLI)

Error: Cannot resolve all parameters for 'Router'(RouteRegistry, Router, ?, Router). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'Router' is decorated with Injectable.

ли я так здесь не так?

ответ

1

После многократного поиска я узнал, что я неправильно вводил поставщиков.

Основываясь на этой ВЕЛИКОЙ article мне удалось решить мою проблему, изменив мою службу к этому:

import { Http } from '@angular/http'; 
import { provide } from '@angular/core'; 
import { SpyLocation } from '@angular/common/testing'; 
import { AuthHttp, JwtHelper } from 'angular2-jwt'; 
import { 
    Router, RootRouter, RouteRegistry, ROUTER_PRIMARY_COMPONENT 
} from '@angular/router-deprecated'; 
import { 
    beforeEach, beforeEachProviders, 
    describe, xdescribe, 
    expect, it, xit, 
    async, inject 
} from '@angular/core/testing'; 
import { UserService } from './user.service'; 

describe('User Service',() => { 

    let service = UserService.prototype; 

    beforeEachProviders(() => [ 
    RouteRegistry, 
    provide(Location, {useClass: SpyLocation}), 
    provide(ROUTER_PRIMARY_COMPONENT, {useValue: UserService}), 
    provide(Router, {useClass: RootRouter}), 
    AuthHttp, 
    Http, 
    UserService 
    ]); 

    it('Should have a login method',() => { 
     expect(service.login).toBeTruthy(); 
    }); 

});