我有一个函数返回一个有很多方法的对象,我需要检查这个返回对象中的一个方法。我使用AngularJS和Karma + Jasmine作为测试套件。如何在由函数返回的对象中调用方法?测试一个函数返回的对象的方法
function modalOptions() {
.........
return this.$q((resolve) => {
// test accessable here
this.solveModel = {
save:() => {
// test can't call save()
this.saveToDB = this.toSendToDB;
},
cancel:() => { ...
},
delete:() => { ...
}
};
});
}
我的测试有点像这样...
it('should save modal with the data', function() {
scope.$apply();
expect(vm.modalOptions).toBeDefined();
vm.toSendToDB = true; // hard-coded
vm.savedToDB = undefined // default value from other controller
spyOn(vm, 'modalOptions').and.callThrough();
console.log(vm.modalOptions()); // gives weird response: c{$$state: Object{status: 0}} instead of the solveModal object
expect(vm.toSendToDB).toBeTruthy();
expect(vm.savedToDB).toBeTruthy();
});
难道你不需要像'vm.modalOptions()。then(function(value){console.log(value);})'得到solveModal对象吗?因为它返回一个承诺不? – Matthias
实际上这是我正在做的事情,但测试并没有进入当时的通话。后来,我只是保持手动调用功能,它的工作。奇怪,但工作:) – Jamie