【发布时间】:2018-01-18 09:43:51
【问题描述】:
我想模拟this._configuration.setLocalStorage_SingleValue()方法
ngOnInit() {
// this.getErrorLog();
this._configuration.setLocalStorage_SingleValue('previousUrl', window.location.href);// throw error
this.pageinationcount = this._configuration.getLocalStorage_SingleValue('previousUrl');
}
这是我的测试方法
it('set/ Get values to local storage', fakeAsync(() => {
console.log(_configuration);
console.log(_mockConfiguration);
spyOn(_configuration, 'setLocalStorage_SingleValue')
.and.callFake(function (key, value) {
_mockConfiguration.setLocalStorage_SingleValue(key, value);
});
spyOn(_configuration, 'getLocalStorage_SingleValue')
.and.callFake(function (key) {
return _mockConfiguration.getLocalStorage_SingleValue(key);// mock data
});
fixture.componentInstance.ngOnInit();
fixture.detectChanges();
expect(fixture.componentInstance.pageinationcount).toEqual("http://localhost:9876/debug.html");
}));
我得到了预期的值,但我在控制台窗口中的这一行 this._configuration.setLocalStorage_SingleValue('previousUrl', window.location.href); 我不知道为什么会抛出这个错误。
Error: Error in :0:0 caused by: Function expected
at DebugAppView.prototype._rethrowWithContext (http://localhost:9876/base/src/test.ts:83266:17)
at DebugAppView.prototype.detectChanges (http://localhost:9876/base/src/test.ts:83239:13)
at ViewRef_.prototype.detectChanges (http://localhost:9876/base/src/test.ts:61222:9)
at ComponentFixture.prototype._tick (http://localhost:9876/base/src/test.ts:25584:13)
at Anonymous function (http://localhost:9876/base/src/test.ts:25598:47)
at ZoneDelegate.prototype.invoke (http://localhost:9876/base/src/test.ts:107920:13)
at ProxyZoneSpec.prototype.onInvoke (http://localhost:9876/base/src/test.ts:70580:13)
at ZoneDelegate.prototype.invoke (http://localhost:9876/base/src/test.ts:107920:13)
at onInvoke (http://localhost:9876/base/src/test.ts:31910:21)
at ZoneDelegate.prototype.invoke (http://localhost:9876/base/src/test.ts:107920:13)
【问题讨论】:
标签: angular unit-testing typescript jasmine karma-jasmine