![](/img/trans.png)
[英]Javascript jest testing a function that was returned inside an object
[英]Testing a method of an object returned by a function
我有一個函數返回一個具有許多方法的對象,我需要檢查此返回對象中的一個方法。 我使用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(); });
抱歉,我還無法發表評論,但必須解決承諾並將solveModel傳遞給它,以便返回solveModel。 你在哪里解決這個承諾?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.