Test a controller with success() and error ()

后端 未结 3 1130
不知归路
不知归路 2021-01-31 10:07

I\'m trying to work out the best way to unit test success and error callbacks in controllers. I am able to mock out service methods, as long as the controller only uses the defa

3条回答
  •  长情又很酷
    2021-01-31 10:55

    Yes, do not use $httpbackend in your controller, because we don't need to make real requests, you just need to make sure that one unit is doing it's job exactly as expected, have a look on this simple controller tests, it's easy to understand

    /**
     * @description Tests for adminEmployeeCtrl controller
     */
    (function () {
    
        "use strict";
    
        describe('Controller: adminEmployeeCtrl ', function () {
    
            /* jshint -W109 */
            var $q, $scope, $controller;
            var empService;
            var errorResponse = 'Not found';
    
    
            var employeesResponse = [
                {id:1,name:'mohammed' },
                {id:2,name:'ramadan' }
            ];
    
            beforeEach(module(
                'loadRequiredModules'
            ));
    
            beforeEach(inject(function (_$q_,
                                        _$controller_,
                                        _$rootScope_,
                                        _empService_) {
                $q = _$q_;
                $controller = _$controller_;
                $scope = _$rootScope_.$new();
                empService = _empService_;
            }));
    
            function successSpies(){
    
                spyOn(empService, 'findEmployee').and.callFake(function () {
                    var deferred = $q.defer();
                    deferred.resolve(employeesResponse);
                    return deferred.promise;
                    // shortcut can be one line
                    // return $q.resolve(employeesResponse);
                });
            }
    
            function rejectedSpies(){
                spyOn(empService, 'findEmployee').and.callFake(function () {
                    var deferred = $q.defer();
                    deferred.reject(errorResponse);
                    return deferred.promise;
                    // shortcut can be one line
                    // return $q.reject(errorResponse);
                });
            }
    
            function initController(){
    
                $controller('adminEmployeeCtrl', {
                    $scope: $scope,
                    empService: empService
                });
            }
    
    
            describe('Success controller initialization', function(){
    
                beforeEach(function(){
    
                    successSpies();
                    initController();
                });
    
                it('should findData by calling findEmployee',function(){
                    $scope.findData();
                    // calling $apply to resolve deferred promises we made in the spies
                    $scope.$apply();
                    expect($scope.loadingEmployee).toEqual(false);
                    expect($scope.allEmployees).toEqual(employeesResponse);
                });
            });
    
            describe('handle controller initialization errors', function(){
    
                beforeEach(function(){
    
                    rejectedSpies();
                    initController();
                });
    
                it('should handle error when calling findEmployee', function(){
                    $scope.findData();
                    $scope.$apply();
                    // your error expectations
                });
            });
        });
    }());
    

提交回复
热议问题