单元测试if / else语句基于promise结果

时间:2015-01-22 19:07:16

标签: angularjs unit-testing jasmine

我正在使用下面列出的功能测试控制器。我的问题是,if else语句是基于promise的结果。如何捕获此承诺的结果以测试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');
    });

红利问题:为什么这个测试没有通过?调用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);
    });

1 个答案:

答案 0 :(得分:0)

在不了解您目前在测试套件中拥有的内容的情况下,我们将如何设置它...

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
});

现在,您可以根据承诺解决的问题来测试条件。例如......

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);
}));