代码之家  ›  专栏  ›  技术社区  ›  Nilesh Mahajan

AngularJs+Jasmin:测试一个函数wch,它不会返回任何东西,而是调用另一个函数进行验证和工厂服务

  •  0
  • Nilesh Mahajan  · 技术社区  · 7 年前

    我正在尝试测试这个调用私有函数的createParameterGroup函数,当我尝试测试这个createParameterGroup函数时,它给出了一个错误,表示validateParameterGroup不是一个函数。

    控制器

    angular.module('PpmApp')
    .controller('parameterGroupListController', ['$scope', '$injector', 'parameterGroups', parameterGroupListController]);
    
      function parameterGroupListController($scope, $injector, parameterGroups) {
        $scope.createParameterGroup = function (parameterGroup) {
          var validationErrors = validateParameterGroup(parameterGroup);
          if (validationErrors.isError) return;
          parameterGroupApiService.createParameterGroup(parameterGroup);
        }
        function validateParameterGroup(parameterGroup) {
          var validationErrors = {};
          validationErrors.isError = false;
         // Validations goes here
         return validationErrors;
        }
    };
    

    测验

    describe('createParameterGroup', function() {
      var validationErrors, parameterGroup;
      beforeEach(function() {
        validationErrors = {};
        validationErrors.isError;
        parameterGroup = {
          GroupName: "ABC",
          Description: "ABC",
          fromMonth: 1,
          fromYear: 18,
          toMonth: 12,
          toYear: 18
        }
      });
    
      it('should create a parameter group', function() {
        expect($scope.createParameterGroup(parameterGroup)).toEqual(false);
      });
    });
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Nilesh Mahajan    7 年前

    在花了一些时间研究如何编写正确的测试用例之后,我发现我在期望错误的条件来评估正确的输出。所以我现在就是这样做的。

    测试用例

    describe('createParameterGroup', function() {
    
      it('with blank name returns error message of property name can not be blank', function() {
        var parameterGroup = {
          name: "",
          description: "sss",
          fromMonth: 1,
          fromYear: 18,
          toMonth: 12,
          toYear: 18
        };
        $scope.createParameterGroup(parameterGroup);
        for (var property in parameterGroup) {
          if (!parameterGroup[property] && property != 'description') {
            var propertyName = (property == 'name') ? 'Parameter group name' : property;
          }
          return property;
        }
        expect($scope.createPopupInfo.validationErrors.isError).toEqual(true);
        expect($scope.createPopupInfo.validationErrors[property]).toEqual(propertyName + ' ' + constantsProvider.validationMessages.blankField);
      });
    
      it('with special characters returns error message of invalid parameter group name', function() {
        var parameterGroup = {
          name: "/*&",
          description: "ABC",
          fromMonth: 1,
          fromYear: 18,
          toMonth: 12,
          toYear: 18
        };
        $scope.createParameterGroup(parameterGroup);
        expect($scope.createPopupInfo.validationErrors.isError).toEqual(true);
        expect($scope.createPopupInfo.validationErrors.name).toEqual('Parameter group name \'' + parameterGroup.name + '\' ' + constantsProvider.validationMessages.specialCharacters);
      });
    
      it('with invalid effective time period returns error message of Invalid effective time period', function() {
        var parameterGroup = {
          name: "ABC",
          description: "ABC",
          fromMonth: 5,
          fromYear: 18,
          toMonth: 4,
          toYear: 18
        };
        $scope.createParameterGroup(parameterGroup);
        expect($scope.createPopupInfo.validationErrors.isError).toEqual(true);
        expect($scope.createPopupInfo.validationErrors.toYear).toEqual(constantsProvider.validationMessages.effectiveTimePeriod);
      });
    
      it('with valid input returns the given input back without any error message', function() {
    
        var parameterGroup = {
          GroupName: "ABC",
          Description: "sss",
          EffectiveStartDateTime: 1 / 18,
          EffectiveEndDateTime: 12 / 18
        };
        var createResponse = {};
        createResponse.IsSuccess = true;
    
        spyOn(parameterGroupApiService, 'createParameterGroup').and.callFake(function() {
          return {
            then: function(callback) {
              return callback(createResponse);
            }
          }
        });
    
        spyOn(parameterGroupApiService, 'getParameterGroups').and.callFake(function() {
          return {
            then: function(callback) {
              callback(parameterGroup);
              return {
                catch: function() {}
              }
            }
          }
        });
    
        $scope.createParameterGroup(parameterGroup);
    
        expect($scope.createPopupInfo.validationErrors.isError).toEqual(false);
        expect(parameterGroupApiService.createParameterGroup).toHaveBeenCalled();
        expect(parameterGroupApiService.getParameterGroups).toHaveBeenCalled();
        expect($scope.parameterGroups).toBe(parameterGroup);
      });
    
    
    });