我想模仿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)
答案 0 :(得分:0)
我明白了。当我将spyon()
代码移到beforeach(),
内部时,错误消失了。
beforeEach(() => {
TestBed.configureTestingModule({
imports: [FormsModule, MessagesModule, DataTableModule],
declarations: [ErrorLogComponent], // declare the test component
providers: [{ provide: ErrorLogService, useClass: ErrorLogService },
{ provide: Configuration, useClass: Configuration }]
}).overrideComponent(ErrorLogComponent, {
set: {
providers: [
{ provide: ErrorLogService, useClass: MockMyService }, { provide: Configuration, useClass: Configuration }
]
}
});
fixture = TestBed.createComponent(ErrorLogComponent);
comp = fixture.componentInstance;
errorListService = fixture.componentRef.injector.get(ErrorLogService);
_configuration = fixture.componentRef.injector.get(Configuration);
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);
});
fixture.detectChanges();
TestBed.compileComponents();
});