Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 基于承诺结果的单元测试if/else语句_Angularjs_Unit Testing_Jasmine - Fatal编程技术网

Angularjs 基于承诺结果的单元测试if/else语句

Angularjs 基于承诺结果的单元测试if/else语句,angularjs,unit-testing,jasmine,Angularjs,Unit Testing,Jasmine,我正在测试一个具有下列功能的控制器。我的问题是,if-else声明是基于承诺的结果。为了测试if-else语句,我如何捕获此承诺的结果?目前我一直得到无法找到变量数据 $scope.get = function (accountId, campaignId, requestId) { campaignBuyRequestService.getCampaignBuyRequest(global.activeOrganizationId, accountId, campaignI

我正在测试一个具有下列功能的控制器。我的问题是,if-else声明是基于承诺的结果。为了测试if-else语句,我如何捕获此承诺的结果?目前我一直得到
无法找到变量数据

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

在不太了解您当前测试套件中的内容的情况下,下面是我如何设置它的

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

在第一个问题中,您的期望应该是查看
scope.data.//…
,而不仅仅是
数据。//…
。同样,对于您的奖金问题,在您的
活动BuyRequestService
中,您将数据分配给
$scope.buyRequest
(单数),但您的测试用例是针对
$scope.buyRequests
(复数)进行测试的,谢谢您的提示!它仍然不太管用,但我的疏忽肯定没有帮助。我首先更新了代码,我认为一个
switch
语句比所有
if…else if
语句要干净得多。其次,我们需要查看
mockBuyRequestService.getActivityBuyRequest
返回的内容。很好,很干净。谢谢,菲尔,这很有帮助