当我尝试在 karma jasmine 中运行测试时,它应该通过 2 个服务。然而,它一直到最后一个函数,但它没有返回它的返回值。有谁知道它如何不返回某些东西?
我之前的每个
function checklistDbFactory(): PouchDB {
// ... is just for sharing
let db = new PouchDB("...");
PouchDB.plugin(PouchFind);
return db;
}
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [
{provide: CHECKLIST_DB, useFactory: checklistDbFactory, deps: []},
DatabaseService,
IndexService,
MockBackend,
BaseRequestOptions,
{
provide: Http,
useFactory: (backend, options) => new Http(backend, options),
deps: [MockBackend, BaseRequestOptions]
}
]
});
backend = TestBed.get(MockBackend);
service = TestBed.get(IndexService);
}));
我的测试本身
it('function should return expectd json', async(() => {
backend.connections.subscribe(connection => {
connection.mockRespond(new Response(<ResponseOptions>{
body: JSON.stringify(expectJson)
}));
});
console.log("getting into main thread");
// ... is just for sharing
service.filldatabase(inputJson, "...").then((data) => {
console.log('getting into filldatabase');
console.log(data);
});
}));
填充数据库函数
filldatabase(jsonfile, key) {
console.log('Getting into filldatabase of 1service');
return this.databaseService.fillPouch(JSON.parse(jsonfile['_body']), key).then( (data) => {
console.log(data);
console.log("Getting into then of fillPouch in 1st service");
return true;
}).catch( () => {
console.log("getting into catch of fillpouch in 1service");
return false;
});
}
填充袋功能
fillPouch(json, key) {
json._id = key;
let push = this.db.put(
json
);
console.log("push");
console.log(push);
return push;
}
IntlliJ 上的测试输出
'getting into main thread'
'Getting into filldatabase of 1service'
'push'
ZoneAwarePromise{__zone_symbol__state: null, __zone_symbol__value: []}
cmd 上的测试输出
✔ Service should be created
LOG: 'getting into main thread'
LOG: 'getting into main thread'
LOG: 'getting into main thread'
LOG: 'getting into main thread'
LOG: 'Getting into filldatabase of 1service'
LOG: 'Getting into filldatabase of 1service'
LOG: 'Getting into filldatabase of 1service'
LOG: 'Getting into filldatabase of 1service'
LOG: 'push'
LOG: 'push'
LOG: 'push'
LOG: 'push'
LOG: ZoneAwarePromise{__zone_symbol__state: null, __zone_symbol__value: []}
LOG: ZoneAwarePromise{__zone_symbol__state: null, __zone_symbol__value: []}
LOG: ZoneAwarePromise{__zone_symbol__state: null, __zone_symbol__value: []}
LOG: ZoneAwarePromise{__zone_symbol__state: null, __zone_symbol__value: []}
. ✔ function should return expectd json
// again file is just for sharing
31 10 2017 13:04:07.375:WARN [web-server]: 404: /assets/XML/<file>.json
LOG: 'Calling getJsonFromFile'
LOG: 'Calling getJsonFromFile'
LOG: 'Calling getJsonFromFile'
LOG: 'Calling getJsonFromFile'
这里也有一些奇怪的东西。Calling getJsonFromFile
。_ 在我的 app.component.ts 里面。但我不会在任何地方调用它。
日志所在的函数是
getData() {
this.databaseService.valueExist('checklistindex').then((data) => {
if(!data) {
console.log("Calling getJsonFromFile");
this.indexService.getJsonfromFile().subscribe((data) => {
console.log(JSON.stringify(data));
this.indexService.filldatabase(data,'checklistindex' );
})
}
})
}
正如你所看到的,我确实进入了我的填充袋。但是它不会返回推送。或任何沿线的东西。