基于 promise 结果的单元测试 if/else 语句
unit testing if/else statement based on promise result
我正在测试具有下列功能的控制器。我的问题是 if else 语句是基于承诺的结果。我如何捕获此承诺的结果以测试 if else 语句?目前我不断收到 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;
});
我的测试是这样的:
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');
});
奖金问题:为什么这个测试没有通过? scope.buyRequest 不应该在调用 get 时自动更新吗?
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);
});
在不太了解您目前在测试套件中拥有的内容的情况下,我将按照以下方式进行设置...
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
});
现在您可以根据 promise 解决的问题测试条件。例如...
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);
}));
我正在测试具有下列功能的控制器。我的问题是 if else 语句是基于承诺的结果。我如何捕获此承诺的结果以测试 if else 语句?目前我不断收到 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;
});
我的测试是这样的:
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');
});
奖金问题:为什么这个测试没有通过? scope.buyRequest 不应该在调用 get 时自动更新吗?
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);
});
在不太了解您目前在测试套件中拥有的内容的情况下,我将按照以下方式进行设置...
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
});
现在您可以根据 promise 解决的问题测试条件。例如...
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);
}));