我对 jasmine (v2.99) 的 angular.io 测试有问题。我为角度服务创建了一个 spyObject 并模拟了一些功能。它按预期工作。现在我想在同一个服务中调用一个原始函数(非模拟)。此函数返回一个承诺。
我已经看到这是用茉莉花中的 callThrough() 完成的。所以,这就是我所做的。它在一定程度上起作用;)
describe('Component2Test',()=>{
let component: Component2Test;
let fixture: ComponentFixture<Component2Test>;
let dataServiceSpy: jasmine.SpyObj<DataService>;
beforeEach(() => {
const dataSrvSpy = jasmine.createSpyObj('DataService', ['getUserObject',
'getLocalStorageObject','getAllLocalStorageKeys','setLocalStorageObject']);
dataSrvSpy.getUserObject.and.returnValue(tua);
dataSrvSpy.getAllLocalStorageKeys.and.returnValue(Promise.resolve(localStorageData));
dataSrvSpy.setLocalStorageObject.and.callThrough(); <--- ?????
}
TestBed.configureTestingModule({
imports: [],
declarations: [Component2Test],
providers: [
{provide: DataService, useValue: dataSrvSpy},
]
});
dataServiceSpy = TestBed.get(DataService);
fixture = TestBed.createComponent(Component2Test);
component = fixture.componentInstance;
});
测试
fit('should set data in localStorage', fakeAsync(() => {
dataServiceSpy.setLocalStorageObject(foundData[foundDataKeys[1]],foundDataKeys[1]).then();
});
尽管调用了原始函数并且我可以看到数据按预期写入。不幸的是,茉莉花返回了这个错误信息
TypeError: Cannot read property 'then' of undefined
服务中的原始功能
setLocalStorageObject(o:object,key:string):Promise<boolean>{
return new Promise((resolve,reject)=>{
this._lclStrgSrv4User.storeJsonDataInLocalStorage(o,key).then((resp)=>{
resolve(resp);
},(err)=>{
reject(err)
});
})
}
所以,我的问题是:
如何正确定义 callThrough() ?
在此先感谢,汤姆