0

我有一个功能需要测试。

         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
                }
            }
        };

我为它写了unitest。

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);

    });

但是当我运行它时,它显示错误: ReferenceError: getPrepaidFieldInfo is not defined

如何检查我的功能并解决此错误。

4

1 回答 1

1

一个原因可能是 using var,这将使您的函数仅在您创建它的范围内定义。例如:

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

如果我试图在bar()外面打电话foo(),那将是未定义的。但是删除varinvar bar使其成为全局并在任何地方定义。所以改变这个:

var getPrepaidFieldInfo = function (field, root) {

至:

getPrepaidFieldInfo = function (field, root) {
于 2015-07-24T04:48:35.323 回答