mardi 30 juin 2015

how can i angular unit test $http combine promise

Im very new in angularjs, so if my question disturb you, i so sorry ! Everything i write is run good, but now i want define a test file for settingcontroller using $httpBackEnd combine with promise ! but it not work ! can anyone help me !

In my case, i using:

var vormetricApp = angular.module('myApp','ngRoute','ngSanitize']);
vormetricApp.config([ '$routeProvider', function($routeProvider) {
    $routeProvider.when('/login', {
            templateUrl : '.....',
            controller  : '.......'
        }).when('/Setting', {
        templateUrl : 'settings.html',
        controller : 'settingController'
    }).when('/Backups', {
        templateUrl : 'app/views/settings/backup/backups.html',
        controller : 'backupController'
    }).when('/ffas', {
       ...................
    }).otherwise({
        redirectTo : '/'
    });
}])

in service i write:

myApp.factory('myservice', function($http, $q) {
    function myservice() {
        this.getSettings = function() {
            var deferred = $q.defer();
            $http({
                method : "GET",
                url : "test/views/settings/settingsdata.json",
            }).success(function(data) {
                deferred.resolve(data);
            }).error(function(msg, code) {
                deferred.reject(msg);
                $log.error(msg, code);
            });
            return deferred.promise;
        };
});

Here is setting controller

myApp.controller('settingController',function($scope, myservice) {

            var result = {};

            myservice.getSettings().then(
            function(pages) {
                $scope.pages = pages;
                result = pages;
            },
            /* error function */
            function(result) {
                console.log("Failed to get data, result is " + result);
            });

            var data = {};
            // Implement search function
            $scope.findValue = function(searchText) {
                angular.forEach(result, function(item) {
                    angular.forEach(item, function(index) {
                        if (index.name.toLowerCase().indexOf(
                                searchText.toLowerCase()) > -1) {
                            if (index.type == "Backup") {
                                if (!data.Backup)
                                    data.Backup = [];
                                data.Backup.push(index);
                            }
                            if (index.type == "System") {
                                if (!data.System)
                                    data.System = [];
                                data.System.push(index);
                            }
                            if (index.type == "Maintenance") {
                                if (!data.Maintenance)
                                    data.Maintenance = [];
                                data.Maintenance.push(index);
                            }
                            if (index.type == "Authentication") {
                                if (!data.Authentication)
                                    data.Authentication = [];
                                data.Authentication.push(index);
                            }
                            if (index.type == "Notification") {
                                if (!data.Notification)
                                    data.Notification = [];
                                data.Notification.push(index);
                            }
                        }
                    });
                });
                $scope.pages = data;
                data = {};
            };
});

My setting data

{  
   "System":[  
      {  
         "link":"#/",
         "name":"Display",
         "content":".............",
         "description":"...............",
         "type":"......"
      },
      {  
         "link":"#/",
         "name":"Email",
         "content":".............",
         "description":"...............",
         "type":"......"
      },
      {  
         "link":"#/",
         "name":"Log Preference",
         "content":".............",
         "description":"...............",
         "type":"......"
      },
      {  
         "link":"#/",
         "name":"Miscellaneous",
         "content":".............",
         "description":"...............",
         "type":"......"
      }
   ],

   "Backup":[  
      {  
         "link":"#/",
         "name":"About",
         "content":".............",
         "description":"...............",
         "type":"......"
      },
      {  
         "link":"#/",
         "name":"Wrapper Keys",
         "content":".............",
         "description":"...............",
         "type":"......"
      },
      {  
         "link":"#/",
         "name":"Backups",
         "content":".............",
         "description":"...............",
         "type":"......"
      },
      {  
         "link":"#/",
         "name":"Restore",
         "content":".............",
         "description":"...............",
         "type":"......"
      }
   ],

   "Maintenance":[  
      {  
         "link":"#",
         "name":"Upload License",
         "content":".............",
         "description":"...............",
         "type":"......"
      },
      {  
         "link":"#",
         "name":"Upgrade",
         "content":".............",
         "description":"...............",
         "type":"......"
      },
      {  
         "link":"#",
         "name":"Software Version",
         "content":".............",
         "description":"...............",
         "type":"......"
      },
      {  
         "link":"#",
         "name":"Network Diagnostics",
         "content":".............",
         "description":"...............",
         "type":"......"
      }
   ],

   "Authentication":[  
      {  
         "link":"#",
         "name":"WebCertificate",
         "content":".............",
         "description":"...............",
         "type":"......"
      },
      {  
         "link":"#",
         "name":"RSA",
         "content":".............",
         "description":"...............",
         "type":"......"
      },
      {  
         "link":"#",
         "name":"LDAP",
         "content":".............",
         "description":"...............",
         "type":"......"
      }
   ],

   "Notification":[  
      {  
         "link":"#",
         "name":"SNMP",
         "content":".............",
         "description":"...............",
         "type":"......"
      },
      {  
         "link":"#",
         "name":"Email",
         "content":".............",
         "description":"...............",
         "type":"......"
      }
   ]   
}

my setting controller test

describe('settingController test', function() {
    var dataSetting = {
        "System" : [ {
            "link" : "#/",
            "name" : "Display",
            "content" : ".............",
            "description" : "...............",
            "type" : "......"
        }, {
            "link" : "#/",
            "name" : "Email",
            "content" : ".............",
            "description" : "...............",
            "type" : "......"
        }, {
            "link" : "#/",
            "name" : "Log Preference",
            "content" : ".............",
            "description" : "...............",
            "type" : "......"
        }, {
            "link" : "#/",
            "name" : "Miscellaneous",
            "content" : ".............",
            "description" : "...............",
            "type" : "......"
        } ],

        "Backup" : [ {
            "link" : "#/",
            "name" : "About",
            "content" : ".............",
            "description" : "...............",
            "type" : "......"
        }, {
            "link" : "#/",
            "name" : "Wrapper Keys",
            "content" : ".............",
            "description" : "...............",
            "type" : "......"
        }, {
            "link" : "#/",
            "name" : "Backups",
            "content" : ".............",
            "description" : "...............",
            "type" : "......"
        }, {
            "link" : "#/",
            "name" : "Restore",
            "content" : ".............",
            "description" : "...............",
            "type" : "......"
        } ],

        "Maintenance" : [ {
            "link" : "#",
            "name" : "Upload License",
            "content" : ".............",
            "description" : "...............",
            "type" : "......"
        }, {
            "link" : "#",
            "name" : "Upgrade",
            "content" : ".............",
            "description" : "...............",
            "type" : "......"
        }, {
            "link" : "#",
            "name" : "Software Version",
            "content" : ".............",
            "description" : "...............",
            "type" : "......"
        }, {
            "link" : "#",
            "name" : "Network Diagnostics",
            "content" : ".............",
            "description" : "...............",
            "type" : "......"
        } ],

        "Authentication" : [ {
            "link" : "#",
            "name" : "WebCertificate",
            "content" : ".............",
            "description" : "...............",
            "type" : "......"
        }, {
            "link" : "#",
            "name" : "RSA",
            "content" : ".............",
            "description" : "...............",
            "type" : "......"
        }, {
            "link" : "#",
            "name" : "LDAP",
            "content" : ".............",
            "description" : "...............",
            "type" : "......"
        } ],

        "Notification" : [ {
            "link" : "#",
            "name" : "SNMP",
            "content" : ".............",
            "description" : "...............",
            "type" : "......"
        }, {
            "link" : "#",
            "name" : "Email",
            "content" : ".............",
            "description" : "...............",
            "type" : "......"
        } ]
    };
    var $httpBackend, $rootScope, createController, settingHandler;
    beforeEach(angular.mock.module("myApp"));
    beforeEach(angular.mock.inject(function($injector) {
        $httpBackend = $injector.get('$httpBackend');
        settingHandler = $httpBackend.when('GET',
                'test/views/settings/settingsdata.json').respond(dataSetting);
        $rootScope = $injector.get('$rootScope');
        var $controller = $injector.get('$controller');
        createController = function() {
            return $controller('settingController', {
                '$scope' : $rootScope
            });
        };
    }));

    afterEach(function() {
        $httpBackend.verifyNoOutstandingExpectation();
        $httpBackend.verifyNoOutstandingRequest();
    });

    it('should fetch get data setting', function() {
        $httpBackend.expectGET('test/views/settings/settingsdata.json');
        var controller = createController();
        $httpBackend.flush();
    });

    it('test function findvalue', inject(function($q, $rootScope) {
        var deferred = $q.defer();
        var promise = deferred.promise;
        var resolvedValue;
        var data = dataSetting;

        promise.then(function(data) {
            resolvedValue = data;
        });
        deferred.resolve(dataSetting);
        var expectSystemData = [ {
            "link" : "#/Display",
            "name" : "Display",
            "content" : "Display this option here",
            "description" : "This is description of the System Settings",
            "type" : "System"
        } ];
        $rootScope.$apply();
        var controller = createController();
        $rootScope.findValue('Display');
        expect($rootScope.pages.System).toEqual(expectSystemData);
    }));
});

I want test function findValue(searchText), but when i run test , i found a testcase is fail The result is:

        Expected undefined to equal [ Object({ link: '#/Display', name: 'Display', content: '.............', description: '...............', type: '......' }) ].
            at Object.<anonymous> (E:/TEST/webapp/test/views/settings/settingcontroller.test1.js:161:35)
            at Object.invoke (E:/TEST/webapp/app/lib/angular/angular.js:4443:17)
            at Object.workFn (E:/TEST/webapp/test/lib/angular/angular-mocks.js:2420:20)
        Error: Unflushed requests: 1
            at Function.$httpBackend.verifyNoOutstandingRequest (E:/TEST/webapp/test/lib/angular/angular-mocks.js:1564:13)
            at Object.<anonymous> (E:/TEST/webapp/test/views/settings/settingcontroller.test1.js:132:16)
Chrome 43.0.2357 (Windows 10 0.0.0): Executed 2 of 2 (1 FAILED) (0.037 secs / 0.03 secs)

Please help me for define a test settingController with using both htppBackend and Promise !!

thanks so much !

Aucun commentaire:

Enregistrer un commentaire