【问题标题】:Location depedency error for PlatformLocationPlatformLocation 的位置依赖性错误
【发布时间】:2016-03-30 22:57:53
【问题描述】:

我的 AppComponent 将 Location(来自 angular2/router)作为依赖项引入。在 AppComponent 中,我调用 Location.path()。在我的 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);
    });

【问题讨论】:

  • specs 中查看他们是如何做到的

标签: javascript jasmine angular


【解决方案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);
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-31
    • 2017-08-28
    • 1970-01-01
    • 2012-01-19
    相关资源
    最近更新 更多