6
考慮和AngularJS應用有:如何在Jasmine for AngularJS中對錶單進行單元測試是否有效?
命名爲A形式 'LoginForm的'
<div class="well well-large loginForm" ng-controller="LoginController" ng-hide="isAuthenticated">
<form id="loginForm" class="form-signin" name="LoginForm">
<h2 class="form-signin-heading">Please sign in</h2>
<input type="text" name="username" ng-model="user.username" required
class="input-block-level input-large" placeholder="Username" autocorrect="off" autocapitalize="off" tabindex="1">
<input type="password" name="password" ng-model="user.password" required
class="input-block-level input-large" placeholder="Password" tabindex="2">
<p ng-show="error401" class="text-error">Username and/or password was incorrect</p>
<p ng-show="error500" class="text-error">Whoops! Something went wrong</p>
<button class="btn btn-primary loginButton" ng-click="login(user)" tabindex="3">Sign in</button>
</form>
</div>
名爲 '的LoginController',用$ scope.login()
$scope.login = function(user) {
if ($scope.LoginForm.$invalid) {
log('LoginForm is invalid');
return;
} else {
log('LoginForm is valid');
}
// ... snip ... other stuff like actual ajax login ...
}
怎麼辦功能的控制器我爲$ scope.LoginForm。$ invalid寫了一個Jasmine測試。
我發現,這是一個只讀屬性,所以我試圖安裝在$範圍模擬對象:
scope.LoginForm = {
"user" : {
"password": "something"
}
};
上面還通過了驗證,即使它不應該有,因爲需要兩個字段。
希望我沒有誤解你,你真的想測試不同的投入,看看他們是否驗證...? :)如果是這樣,你應該檢查e2e測試。 –