PlatformLocation的位置依赖性错误

时间:2016-03-30 22:57:54

标签: javascript jasmine angular

我有我的AppComponent,它将位置(来自angular2 /路由器)作为依赖项。在AppComponent中,我正在调用Location.path()。在我的Jasmine测试中,我看到以下错误。我的Jasmine测试有什么问题?如何设置依赖项?

堆栈跟踪:

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规范:

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);
    });

1 个答案:

答案 0 :(得分:1)

好的,我明白了。首先,我必须导入SpyLocation对象,然后调整我的引用。下面的工作代码。

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);
    });