2015-10-09 74 views
0

我第一次嘗試茉莉花單元測試並將其作爲現有代碼應用,我試圖按照茉莉花的文檔。茉莉花測試參考錯誤

下面是HTML文件

<!doctype html> 
<html> 
    <head> 
     <title>Jasmine Test</title> 
     <link rel="stylesheet" href="bower_components/jasmine/lib/jasmine-core/jasmine.css"> 
    </head> 
    <body> 
     <script src="bower_components/jasmine/lib/jasmine-core/jasmine.js"></script> 
     <script src="bower_components/jasmine/lib/jasmine-core/jasmine-html.js"></script> 
     <script src="bower_components/jasmine/lib/jasmine-core/boot.js"></script> 

     <!-- include source files here... --> 
     <script type="text/javascript" src="signUp.js"></script> 

     <!-- include spec files here... --> 
     <script src="test/signUp.js"></script> 
    </body> 
</html> 

以下的結構是在我signUp.js代碼文件

var signUp = { 

    usernameValid: function(validity){ 
     var emailValue = $('#inputEmail').val(), 
      pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i), 
      regexVal = pattern.test(emailValue); 
     if(emailValue == "" || !regexVal){ 
      $('.inputEmail').removeClass("has-success").addClass("has-error"); 
     }else{ 
      validity = true 
      $('.inputEmail').removeClass("has-error").addClass("has-success"); 
     } 

     return validity; 
    }, 

    passwordValidity: function(validity){ 
     var passwordVal = $('#inputPassword').val(); 

     if(passwordVal == ""){ 
      $('.inputPassword').addClass("has-error").removeClass("has-success"); 
     }else if(passwordVal.length < 6){ 
      $('.inputPassword').addClass("has-error").removeClass("has-success"); 
      $('#inputPassword').attr("placeholder", "Password must be at least 6 characters"); 
     }else{ 
      validity = true; 
      $('.inputPassword').addClass("has-success").removeClass("has-error"); 
     } 

     return validity; 
    }, 

    cnfrmPassword: function(validity){ 
     var confirmPassVal = $('#inputConfirmPassword').val(), 
      passwordVal = $('#inputPassword').val(); 

     if(confirmPassVal == "" || passwordVal != confirmPassVal){ 
      $('.inputConfirmPassword').addClass("has-error").removeClass("has-success"); 
      $('#inputConfirmPassword').attr("placeholder", "Password did not match"); 
     }else{ 
      validity = true; 
      $('.inputConfirmPassword').addClass("has-success").removeClass("has-error"); 
     } 

     return validity; 
    }, 

    nameValidity: function(validity){ 
     var firstName = $('#inputFirstName').val(), 
      pattern = new RegExp(/^[a-zA-Z]*$/), 
      regexVal = pattern.test(firstName); 

     if(firstName == "" || !regexVal){ 
      $('.inputFirstName').addClass("has-error").removeClass("has-success"); 
     }else if(!regexVal){ 
      $('.inputFirstName').addClass("has-error").removeClass("has-success"); 
      $('#inputFirstName').attr("placeholder", "Only letters for this field"); 
     }else{ 
      validity = true; 
      $('.inputFirstName').addClass("has-success").removeClass("has-error"); 
     } 

     return validity; 
    }, 

    lastNameField: function(){ 
     var lastName = $('#inputLastName').val(); 

     if(lastName){ 
      $('.inputLastName').addClass("has-success"); 
     } 
    }, 

    checkDOB: function(validity){ 
     var dateOfBirth = $('#inputDOB').val(), 
      today = new Date(), 
      birthdate = new Date(dateOfBirth), 
      age = today.getFullYear() - birthdate.getFullYear(), 
      month = today.getMonth() - birthdate.getMonth(); 

     if (month < 0 || (month == 0 && today.getDate() < birthDate.getDate())) { 
      age--; 
     } 

     if(dateOfBirth == "" || (age < 14 || age > 150)){ 
      $('.inputDOB').addClass("has-error").removeClass("has-success"); 
     }else{ 
      validity = true; 
      $('.inputDOB').addClass("has-success").removeClass("has-error"); 
     } 

     return validity; 
    }, 

    convertDate: function(dateOfBirth){ 
     var date = new Date(dateOfBirth); 
     return date.toISOString(); 
    }, 

    dateType: function(){ 
     if(!Modernizr.inputtypes.date){ 
      $('#inputDOB').datepicker(); 
     } 
    } 

$(document).ready(function(){ 
    signUp.usernameValid(); 
    //any other functionality 
} 

在我的測試/ signUp.js文件,我有以下

describe("SignUp fields", function(){ 
    it("should expect UserName", function(){ 
     expect(signUp.usernameValid()).toBeDefined(); 
    }); 
}); 

運行上述我似乎得到以下錯誤

類型錯誤:無法在檢查「註冊」讀的不確定

而且財產「usernameValid」對象,它拋出一個引用錯誤

編輯:是否有與jQuery需要任何特殊的支持茉莉?

回答

0

因此,經過一點研究,我發現我錯過了jquery文件。我包含了來自cdn的jquery源碼擴展,但我仍然注意到同樣的錯誤,然後我切換到本地版本的jquery,它的工作。此外,我不得不包括現代化庫,因爲我正在使用它在我的主要js文件。