gpt4 book ai didi

javascript - .callFake 与 $q 仍然不进入 .then block

转载 作者:行者123 更新时间:2023-11-28 04:57:37 25 4
gpt4 key购买 nike

我之前在其他 Controller 上编写过一些这样的测试,并且效果很好。但在这个更复杂的 Controller 上,无论我在哪个函数上测试它,使用 $q.callFake 都不会进入 .then block ,因此根本没有执行任何逻辑。我知道 Controller 肯定可以工作,因为这是在生产网站上。那么,当我返回延迟的 Promise 时,为什么我的测试甚至没有进入 .then block 呢?

Controller 代码 -

(function() {
'use strict';

angular
.module('myApp')
.controller('EntryAddController', EntryAddController);

function EntryAddController($scope, $state, moment, $sanitize, EntryFactory, $modal, toastr, $log) {
var vm = this;
var now = moment();
var $currentYear = now.year();

vm.queues = [];
vm.calculating = false;
vm.total = 0;

vm.saving = false;
vm.addTxt = 'Confirm Entry';

var tomorrow = now.add(1, 'days').toDate();
var to = now.subtract(1, 'days').add(12, 'months').toDate();

vm.fromDate = tomorrow;
vm.toDate = to;

activate();

////////////////

function activate() {

var queueCache = {};

vm.updateEntrys = function() {

var payload = {
'from': moment(vm.fromDate).format('MM/DD/YYYY'),
'to': moment(vm.toDate).format('MM/DD/YYYY'),
'freq': vm.frequency.value,
'total': vm.total_amount
};

var key = JSON.stringify(payload);

if (!(key in queueCache)) {

EntryFactory.getEntryQueue(payload)
.then(function(resp) {

//LOGIC HERE BUT TEST NEVER ENTERS HERE DESPITE USING $Q
});

} else {

vm.queues = queueCache[key].queue;
vm.total = queueCache[key].total;
vm.calculating = false;
}
}
}
}
})();

测试代码

(function() {
'use strict';

describe('Entry Add Controller Spec', function(){

var vm;
var $controller;
var $q;
var $rootScope;
var EntryFactory;
var $scope;
var toastr;

beforeEach(module('myApp'));

beforeEach(inject(function(_$controller_, _$q_, _$rootScope_, _EntryFactory_) {
$controller = _$controller_;
$rootScope = _$rootScope_;
$scope = _$rootScope_.$new();
$q = _$q_;
EntryFactory = _EntryFactory_;

vm = $controller('EntryAddController', { $scope: $scope });
}));

it('expect EntryFactory.getEntryQueue to correctly set queues and total upon successful response', function() {
var payload = "blah";
var resp = {
"data": 1
}

spyOn(EntryFactory, 'getEntryQueue').and.callFake(function(payload) {
var deferred = $q.defer();
deferred.resolve(resp);
return deferred.promise;
});

EntryFactory.getEntryQueue(payload);

$rootScope.$apply();

//expect logic in .then to have worked

});
});
})();

编辑

只是想到了一件事......这是因为我直接在测试中调用工厂函数(EntryFactory.getEntryQueue),而不是调用vm.updateEntrys > 围绕它运行,因此它不会继续执行代码的 .then 部分?

最佳答案

您始终需要考虑您的 SUT(被测系统/软件)是什么。在本例中,它是 Controller 的组件方法 vm.updateEntrys。它调用您的 Mocked EntryFactory.getEntryQueue() 方法。另外您的代码不完整(您在哪里定义 vm.Frequency 对象?)。尽管如此,我已经通过以下测试测试了该方法:

    /*property
$apply, $log, $modal, $new, $sanitize, $scope, $state, EntryFactory, and,
callFake, data, defer, getEntryQueue, moment, promise, resolve, toastr,
updateEntrys
*/
(function () {
'use strict';

describe('Entry Add Controller Spec', function () {

var vm;
var $controller;
var $q;
var $rootScope;
var EntryFactory;
var $scope;
var toastr;

beforeEach(module('app.controllers'));

beforeEach(inject(function (_$controller_, _$q_, _$rootScope_) {
$controller = _$controller_;
$rootScope = _$rootScope_;
$q = _$q_;

}));

beforeEach(function () {
$scope = $rootScope.$new();

EntryFactory = {
getEntryQueue: function () {

}
};

moment = function () {
return {
format: function () {

}
}
}

vm = $controller('EntryAddController', { '$scope': $scope, '$state': {}, 'moment': moment, '$sanitize': {}, 'EntryFactory': EntryFactory, '$modal': {}, 'toastr': {}, '$log': {} });
});

it('expect EntryFactory.getEntryQueue to correctly set queues and total upon successful response', function () {
var payload = "blah";
var resp = {
"data": 1
};

spyOn(EntryFactory, 'getEntryQueue').and.callFake(function (payload) {
var deferred = $q.defer();
deferred.resolve(resp);
return deferred.promise;
});

vm.updateEntrys();

EntryFactory.getEntryQueue(payload);

$scope.$apply();

//expect logic in .then to have worked
});
});

})();

关于javascript - .callFake 与 $q 仍然不进入 .then block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42411034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com