@angular/common 中的位置 class 在单元测试中不起作用
Location class from @angular/common doesn't work in unit tests
出于某种原因,Location.normalize() 和其他函数在我 运行 测试时 return 没有正确响应:
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"
为什么要规范化 returns null?根据文档:
归一化(url:字符串):字符串;
给定一个表示 URL 的字符串,return 是规范化的 URL 路径,没有前导或尾随斜杠。
因为 RouterTestingModule provides a different implementation, SpyLocation, of the Location service. And this implementation always returns 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([])
出于某种原因,Location.normalize() 和其他函数在我 运行 测试时 return 没有正确响应:
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"
为什么要规范化 returns null?根据文档:
归一化(url:字符串):字符串;
给定一个表示 URL 的字符串,return 是规范化的 URL 路径,没有前导或尾随斜杠。
因为 RouterTestingModule provides a different implementation, SpyLocation, of the Location service. And this implementation always returns 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([])