[英]unit testing if/else statement based on promise result
I am testing a controller with the function listed below. 我正在测试具有以下功能的控制器。 My problem is that the if else statment is based on the result of a promise. 我的问题是,if else陈述是基于诺言的结果。 How can I capture the result of this promise in order to test the if else statements? 我如何捕获此诺言的结果以测试if else语句? At the moment I keep getting Can't find variable data
此刻我不断得到Can't find variable data
$scope.get = function (accountId, campaignId, requestId) {
campaignBuyRequestService.getCampaignBuyRequest(global.activeOrganizationId, accountId, campaignId, requestId).then(function (data) {
if (data) {
$.each(data.requestActions, function (index) {
if (data.requestActions[index].statusCode === 10001000) {
data.requestActions[index].statusCodeDisplayText = "Sent";
data.requestActions[index].iconClass = "fa fa-arrow-left";
} else if (data.requestActions[index].statusCode === 10002000) {
data.requestActions[index].statusCodeDisplayText = "Opted In";
data.requestActions[index].iconClass = "fa fa-arrow-right";
} else if (data.requestActions[index].statusCode === 10003000) {
data.requestActions[index].statusCodeDisplayText = "Rejected";
data.requestActions[index].iconClass = "fa fa-exclamation-circle";
} else if (data.requestActions[index].statusCode === 10004000) {
data.requestActions[index].statusCodeDisplayText = "Withdrawn";
data.requestActions[index].iconClass = "fa fa-exclamation-circle";
} else if (data.requestActions[index].statusCode === 10005000) {
data.requestActions[index].statusCodeDisplayText = "Activated";
data.requestActions[index].iconClass = "fa fa-play";
} else if (data.requestActions[index].statusCode === 10006000) {
data.requestActions[index].statusCodeDisplayText = "Canceled";
data.requestActions[index].iconClass = "fa fa-exclamation-circle";
} else if (data.requestActions[index].statusCode === 10007000) {
data.requestActions[index].statusCodeDisplayText = "Paused";
data.requestActions[index].iconClass = "fa fa-pause";
} else {
data.requestActions[index].statusCodeDisplayText = "Unknown";
data.requestActions[index].iconClass = "fa fa-question";
}
});
}
$scope.buyRequest = data;
});
My test looks like this: 我的测试看起来像这样:
it('should assign statusCodeDisplayText and Icon class based on status code', function () {
spyOn(scope, "get");
scope.get(buyRequest.requestId, mockStateParams.accountId, mockStateParams.campaignId);
scope.data = spyOn(mockBuyRequestService, 'getCampaignBuyRequest').and.callThrough;
scope.$digest();
expect(scope.data.requestActions[0].statusCodeDisplayText).toEqual('Sent');
expect(scope.data.requestActions[0].iconClass).toEqual('fa fa-arrow-left');
expect(scope.data.requestActions[1].statusCodeDisplayText).toEqual('Withdrawn');
expect(scope.data.requestActions[1].iconClass).toEqual('fa fa-exclamation-circle');
});
bonus question: why does this test not pass? 奖金问题:为什么这个测试不及格? Shouldn't scope.buyRequest be automaticly updated when get is called? 调用get时,scope.buyRequest是否应该自动更新吗?
it('should make a request for campaigns and assign them to buyRequests', function () {
scope.get(mockStateParams.accountId, mockStateParams.campaignId, buyRequest.requestId);
scope.$digest();
expect(scope.buyRequest).toEqual(buyRequest);
});
Without knowing much about what you currently have in your test suite, here's how I'd set it up... 在不了解测试套件中当前所具有的功能的情况下,这是我的设置方法...
var campaignBuyRequestService, deferred, scope;
beforeEach(function() {
campaignBuyRequestService = jasmine.createSpyObj('campaignBuyRequestService', ['getCampaignBuyRequest']);
module('module-that-you-are-testing', function($provide) {
$provide.value('campaignBuyRequestService', campaignBuyRequestService);
});
inject(function($q) {
deferred = $q.defer();
campaignBuyRequestService.getCampaignBuyRequest.and.returnValue(deferred.promise);
});
// assign scope however you're doing it already
});
Now you can test the conditions based on what's resolved by the promise. 现在,您可以根据诺言所解决的问题测试条件。 For example... 例如...
it('sent status', inject(function($rootScope) {
var sentAction = { statusCode: 10001000 },
data = {
requestActions: [sentAction]
};
deferred.resolve(data);
scope.get(...);
expect(campaignBuyRequestService.getCampaignBuyRequest).toHaveBeenCalled();
$rootScope.$apply(); // call this to run a digest cycle and resolve promises
expect(sentAction.statusCodeDisplayText).toBe('Sent');
expect(sentAction.iconClass).toBe('fa fa-arrow-left');
expect(scope.buyRequest).toBe(data);
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.