如何测试$ ionicPopup.show

时间:2018-02-04 23:58:58

标签: ionic-framework jasmine

我有一个像这样的ionicPopup:

    it('Should edit notifications', function() {
        spyOn($ionicPopup, 'show').and.callFake(function() {
            return $q.resolve(true);
        })
        spyOn(localStorageManager, 'set');

        controller = createController();
        controller.editNotifications();
        $scope.$apply();

        expect(localStorageManager.set).toHaveBeenCalled();
        // expect(localStorageManager.set).toHaveBeenCalledWith('notificationStatus');
    });

这是我到目前为止所拥有的;

{{1}}

我基本上不知道如何测试这个东西。互联网似乎并不多。我想我需要在callFake函数中做一些事情,但我有点卡住了。有没有人曾经成功测试过这种野兽?

1 个答案:

答案 0 :(得分:0)

通过以下答案:https://stackoverflow.com/a/52565500/7943457

并修改您的代码,如下所示:

function editNotifications() {
        var popUp = $ionicPopup.show({
            template: '<ion-checkbox ng-repeat="item in vm.notifications" ng-model="item.notification" ng-checked="item.notification">' +
            '<span class="notificationsFont">{{item.settingsText}}</span></ion-checkbox>',
            title: 'Notification Settings',
            scope: $scope,
            buttons: [ 
            {
                text: 'Cancel',
                onTap: function(e) {
                    return false;
                }
            },
            {
                text: '<b>Ok</b>',
                type: 'button-positive',
                onTap: function(e) {
                    return true;
                }

            }]
            });
            popup.then(function(response){
              if(response){ //when response is true
                 localStorageManager.set('notificationStatus', vm.notifications);
                 vm.notificationText = "Notifications off";
                 setNotificationValuesBasedOnUserInput(vm.notifications);
              }else{ //when response is false
                 vm.notifications = localStorageManager.get('notificationStatus');
              }
            })
    };

您可以像这样测试它:

it('Should edit notifications', function() {
    spyOn($ionicPopup, 'show').and.callFake(function() {
        return $q.resolve(true);
    })
    spyOn(localStorageManager, 'set');

    controller = createController();
    controller.editNotifications();
    $scope.$apply();

    expect(localStorageManager.set).toHaveBeenCalled();
    // expect(localStorageManager.set).toHaveBeenCalledWith('notificationStatus');
});

it('Should get notifications', function() {
    spyOn($ionicPopup, 'show').and.callFake(function() {
       return $q.resolve(false);
    })
    spyOn(localStorageManager, 'get');

    controller = createController();
    controller.editNotifications();
    $scope.$apply();
    expect(localStorageManager.get).toHaveBeenCalled();
        // expect(localStorageManager.get).toHaveBeenCalledWith('notificationStatus');
});

希望有帮助。