0

I have a function need to test.

         var getPrepaidFieldInfo = function (field, root) {
            if (field === 'PatientDOB') {
                return {
                    fieldName: root.localizedStrings.patientDobLabel,
                    fieldValue: $filter('date')(new Date(root.principal.patientDob), 'MM/dd/yyyy')
                }
            }
            if (field === 'PatientLastName') {
                return {
                    fieldName: root.localizedStrings.patientLastNameLabel,
                    fieldValue: root.principal.lastName
                }
            }
            if (field === 'ZipCode') {
                return {
                    fieldName: root.localizedStrings.postalCodeLabel,
                    fieldValue: root.principal.zipCode
                }
            }
            if (field === 'Client') {
                return {
                    fieldName: root.localizedStrings.clientIdLabel,
                    fieldValue: root.principal.clientId
                }
            }
        };

And i write unitest for its.

it('getPrepaidFieldInfo should work properly', function(){
        expect(getPrepaidFieldInfo).toBeDefined();
        var field, actual;

        field === 'PatientDOB';
        spyOn(window.getPrepaidFieldInfo).and.CallThrough();
        actual = getPrepaidFieldInfo(field,$rootScope);
        expect(actual.fieldName).toEqual($rootScope.localizedStrings.patientDobLabel);

    });

But when i run it, it's shown error: ReferenceError: getPrepaidFieldInfo is not defined

How to check my function and solve this error.

Evan Carslake
  • 2,267
  • 15
  • 38
  • 56
Pham Minh Tan
  • 2,076
  • 7
  • 25
  • 37

1 Answers1

1

A reason could be using var, which would make your function only be defined in the scope you created it in. For example:

var foo = function() {
    var bar = function() { 
        ...
    }
    ...
}

If I tried to call bar() outside foo() it would be undefined. But removing the var in var bar makes it global and defined anywhere. So change this:

var getPrepaidFieldInfo = function (field, root) {

to:

getPrepaidFieldInfo = function (field, root) {
Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54