lundi 22 février 2016

Karma Jasmine execute a test from a helper function

I'm using Jasmine w/ Karma for unit testing my Angular application. I want to be able to validate HTML fields. I have a fieldvalidate.js as below

var FieldType = Object.freeze({'PERSON_NAME': 1, 'PHONE_NUMBER': 2})

var FieldValidator = function () {
    console.log('fv create');
}

FieldValidator.prototype = Object.create({}, {

    test: {
        value: function () {
            console.log('test');
        }
    },

    validate: {
        value: function (field, fieldType, required) {
            console.log('validate: ' + fieldType);
            console.log('field: ' + field);

            it('should require this to pass', function () {
                console.log("Field Validator Test.");
            });

        }
    }
});

I have my spec.js calling the fieldvalidator to execute validations on a field.

 describe('register.html page', function () {

    beforeEach(mockAnyApiCall);

    var $scope;
    var form;
    var createController;

    beforeEach(inject(function ($injector) {

        $scope = $injector.get('$rootScope').$new();
        var locals = {
            '$scope': $scope
        };
        createController = function () {
            $injector.get('$controller')('RegisterController', locals);
        };

        createController();

        var $templateCache = $injector.get('$templateCache');
        var templateHtml = $templateCache.get('scripts/app/account/register/register.html')
        var formElem = angular.element("<div>" + templateHtml + "</div>")
        var $compile = $injector.get('$compile');
        $compile(formElem)($scope);
        form = $scope.form;

        $scope.$apply()
    }));

    fdescribe('validation tests - inner', function (){

        beforeEach(function(){

        });

        it('should test', function(){
            var fieldValidator = new FieldValidator();
            fieldValidator.validate(form.inputFirstName, FieldType.PERSON_NAME, true);
        })

I see FieldValidator.validate getting called but the test inside validate is not getting run. How do I get this working ? I needed to use fieldValidator from 'it' because form is not accessible directly from inner describe.

Aucun commentaire:

Enregistrer un commentaire