2016-03-30 47 views
0

Konumuma (angular2/yönlendiriciden) bağımlılık olarak giren AppComponent'im var. AppComponent içinde Location.path() çağırıyorum. Yasemin testimde, şu hatayı görüyorum. Jasmine testimin nesi var? Bağımlılıkları nasıl kurarım?PlatformLocation için konum bağımlılık hatası

stacktrace:

Error: EXCEPTION: Error during instantiation of PlatformLocation! (Location -> LocationStrategy -> PlatformLocation). 
ORIGINAL EXCEPTION: TypeError: Cannot read property 'getLocation' of null 
ORIGINAL STACKTRACE: 
TypeError: Cannot read property 'getLocation' of null 
    at BrowserPlatformLocation._init (http://localhost:3000/libs/angular2/bundles/router.dev.js:2252:41) 
    at new BrowserPlatformLocation (http://localhost:3000/libs/angular2/bundles/router.dev.js:2249:12) 
    at http://localhost:3000/libs/angular2/bundles/angular2.dev.js:1519:20 
    at Injector._instantiate (http://localhost:3000/libs/angular2/bundles/angular2.dev.js:11127:19) 
    at Injector._instantiateProvider (http://localhost:3000/libs/angular2/bundles/angular2.dev.js:11069:21) 
    at Injector._new (http://localhost:3000/libs/angular2/bundles/angular2.dev.js:11059:19) 
    at InjectorDynamicStrategy.getObjByKeyId (http://localhost:3000/libs/angular2/bundles/angular2.dev.js:10937:42) 
    at Injector._getByKeyDefault (http://localhost:3000/libs/angular2/bundles/angular2.dev.js:11260:33) 
    at Injector._getByKey (http://localhost:3000/libs/angular2/bundles/angular2.dev.js:11214:21) 
    at Injector._getByDependency (http://localhost:3000/libs/angular2/bundles/angular2.dev.js:11202:21) 

AppComponent Spec:

import {describe, it, expect, beforeEach, afterEach, beforeEachProviders, MockApplicationRef} from "angular2/testing"; 

import {provide, Injector, ApplicationRef} from "angular2/core"; 
import {XHRBackend, HTTP_PROVIDERS} from "angular2/http"; 
import {MockConnection, MockBackend} from "angular2/src/http/backends/mock_backend"; 
import {ROUTER_PROVIDERS, APP_BASE_HREF, ROUTER_PRIMARY_COMPONENT, Location, LocationStrategy, HashLocationStrategy} from "angular2/router"; 

import {TRANSLATE_PROVIDERS, TranslateService} from "ng2-translate/ng2-translate"; 

import {AppComponent} from "./AppComponent"; 


describe("AppComponent",() => { 
    let injector: Injector; 
    let translateService: TranslateService; 
    let app: AppComponent; 
    let location: Location; 

    beforeEachProviders(() => [ 
     ROUTER_PROVIDERS, 
     provide(ROUTER_PRIMARY_COMPONENT, {useClass: MockPrimaryComponent}), 
     provide(APP_BASE_HREF, {useValue: "/"}), 
     provide(LocationStrategy, { useClass: HashLocationStrategy }), 
     Location, 
     provide(ApplicationRef, {useClass: MockApplicationRef}) 
    ]); 

    beforeEach(() => { 
     injector = Injector.resolveAndCreate([ 
      HTTP_PROVIDERS, 
      ROUTER_PROVIDERS, 
      Location, 
      TRANSLATE_PROVIDERS, 
      TranslateService 

     ]); 

     location = injector.get(Location); 

     translateService = injector.get(TranslateService); 

     app = new AppComponent(translateService, location); 
    }); 
+0

onların [özellikleri] bu işler nasıl Bkz (https://github.com/angular/angular/blob/f9fb72fb0e9bcbda7aeebbf8321ce5d70d78ecee/modules/angular2/test/router/integration/util.ts#L52) –

cevap

1

Tamam, bunu anladım. İlk olarak, SpyLocation nesnesini almam ve ardından referanslarımı ayarlamam gerekiyordu. Aşağıdaki çalışma kodu.

import {describe, it, expect, beforeEach, afterEach, beforeEachProviders, MockApplicationRef} from "angular2/testing"; 

import {provide, Injector, ApplicationRef} from "angular2/core"; 
import {XHRBackend, HTTP_PROVIDERS} from "angular2/http"; 
import {MockConnection, MockBackend} from "angular2/src/http/backends/mock_backend"; 
import {ROUTER_PROVIDERS, APP_BASE_HREF, ROUTER_PRIMARY_COMPONENT, Location, LocationStrategy, HashLocationStrategy} from "angular2/router"; 
import {SpyLocation} from "angular2/src/mock/location_mock"; 

import {TRANSLATE_PROVIDERS, TranslateService} from "ng2-translate/ng2-translate"; 

import {AppComponent} from "./AppComponent"; 


describe("AppComponent",() => { 
    let injector: Injector; 
    let translateService: TranslateService; 
    let app: AppComponent; 
    let location: SpyLocation; 

    beforeEach(() => { 
     injector = Injector.resolveAndCreate([ 
      HTTP_PROVIDERS, 
      ROUTER_PROVIDERS, 
      // Location, 
      provide(Location, {useClass: SpyLocation}), 
      TRANSLATE_PROVIDERS, 
      TranslateService 
     ]); 

     location = injector.get(Location); 

     translateService = injector.get(TranslateService); 

     app = new AppComponent(translateService, location); 
    }); 
İlgili konular