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

标签 angular angular2-components angular2-testing angular2-cli

由于某些原因,当我运行测试时 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"

为什么 normilize 返回 null?根据文档:

规范化(url:字符串):字符串;

给定一个表示 URL 的字符串,返回没有前导或尾随斜杠的规范化 URL 路径。

最佳答案

因为 RouterTestingModule provides a different implementation , SpyLocation, 定位服务。而这个实现 always returns null .

关于angular - 来自@angular/common 的位置类在单元测试中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47484775/

相关文章:

css - 如何使用 bootstrap 4 数据表更改 Angular 4 中的背景、行、行选择和标题的颜色

Angular 2组件.html路径

Angular2获取激活的路由参数和父参数

Angular 2/Ionic 2 TypeError : zxcvbn_1. 默认不是函数

javascript - 订阅后将值保存到数组或变量中

javascript - Angular2 - 使用 debounceTime 测试调用

Angular/Rxjs 条件嵌套 Observables

angular - 兄弟组件之间如何通信

angular - 测试: blur emulate

unit-testing - TestBed:通过模块导入时组件不编译