问题
I am trying to test this createParameterGroup function which is calling a private function, When I try to test this createParameterGroup function, it gives an error saying that validateParameterGroup is not a function.
controller
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;
}
};
Test
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:
After spending some time around how to write test cases right, I figured it out that I was doing expecting the wrong condition to evaluate the correct output. So this is how I have done it now.
Test Cases
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);
});
});
来源:https://stackoverflow.com/questions/48811341/angularjs-jasmin-testing-a-function-wchich-does-not-return-anything-but-call