来自 @angular/common 的位置类在单元测试中不起作用



出于某种原因location.normalize()和其他功能在我运行测试时不会返回正确的响应:

import { Router } from '@angular/router';
import { Location } from "@angular/common";
import { TestBed, async, tick } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
describe('Location', () => {
  let location: Location;
  let router = {
    navigate: jasmine.createSpy('navigate')
  };
  beforeEach(async(() => {
    TestBed.configureTestingModule(
      {
        declarations: [],
        providers: [
          {
            provide: Router,
            useValue: router
          }
        ],
        imports: [
          RouterTestingModule
        ]
      }
    ).compileComponents();
    location = TestBed.get(Location);
  }));
  it('TEST', async(() => {
    expect(location.normalize('/test/')).toBe('test');
  }));
});

控制台日志:

Chrome 61.0.3163 (Linux 0.0.0) Location TEST FAILED
    Expected null to be 'test'.
        at new ZoneAwareError home/yuriy/projects/frontend/checkout/node_modules/zone.js/dist/zone.js:917:1)
        at Object.<anonymous> home/yuriy/projects/frontend/checkout/src/modules/checkout/services/finished-flow.guard.spec.ts:143:42)
        at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke home/yuriy/projects/frontend/checkout/node_modules/zone.js/dist/zone.js:365:1)
        at AsyncTestZoneSpec.webpackJsonp.../../../../zone.js/dist/async-test.js.AsyncTestZoneSpec.onInvoke home/yuriy/projects/frontend/checkout/node_modules/zone.js/dist/async-test.js:49:1)
        at ProxyZoneSpec.webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke home/yuriy/projects/frontend/checkout/node_modules/zone.js/dist/proxy.js:76:1)
        at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke home/yuriy/projects/frontend/checkout/node_modules/zone.js/dist/zone.js:364:1)
        at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runGuarded home/yuriy/projects/frontend/checkout/node_modules/zone.js/dist/zone.js:138:1)
        at runInTestZone home/yuriy/projects/frontend/checkout/node_modules/@angular/core/bundles/core-testing.umd.js:110:1)
        at Object.<anonymous> home/yuriy/projects/frontend/checkout/node_modules/@angular/core/bundles/core-testing.umd.js:49:1)
Chrome 61.0.3163 (Linux 0.0.0): Executed 3 of 3 (1 FAILED) (0 secs / 0.385 secs)

依赖项:

"@angular/cli": "1.3.0",
"@angular/common": "2.4.7",
"@angular/compiler": "2.4.7",
"@angular/compiler-cli": "2.4.7",
"@angular/core": "2.4.7",
"@angular/forms": "2.4.7",
"@angular/http": "2.4.7",
"@angular/platform-browser": "2.4.7",
"@angular/platform-browser-dynamic": "2.4.7",
"@angular/router": "3.4.7",
"karma": "~1.7.0",
"karma-chrome-launcher": "~2.1.1",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"zone.js": "0.8.5"

为什么要标准化返回null?加入文档:

归一化(url:string):string;

给定一个代表URL的字符串,返回归一化的URL路径而不引导或落后。

,因为routerTestingModule提供了位置服务的不同实现,加音。此实现总是返回null。

我有一个糟糕的时间试图在Angular 6中模拟Location(我使用它来更改URL而不重新加载组件):

NullInjectorError: No provider for Location!

,如果我将位置添加到imports

Unexpected value 'Location' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.

JB Nizet的答案帮助我解决了

// In the component .spec imports
RouterTestingModule.withRoutes([])

最新更新