0
這裏是控制器和I只是想測試,它被定義我需要編寫爲通過所定義
'use strict';
mainApp.controller('HeaderCtrl',
function ($scope, sessionSrvc, eventSrvc, $state) {
// Initially keep both SingIn and SignOut as hidden
// until it's determined if the session is alive or not.
var user = sessionSrvc.getCurrentUser();
if(user.fullName != undefined) {
$scope.signInVisible = false;
$scope.signOutVisible = true;
} else {
user.then(function(user) {
if(user != null) {
$scope.user = user;
$scope.signInVisible = false;
$scope.signOutVisible = true;
} else {
$scope.signInVisible = true;
$scope.signOutVisible = false;
}
}, function(errorId) {
// alert(errorId);
});
}
/**
* This callback is called when the the user successfully logs in
* and the signIn dialog closes.
*/
$scope.$on(eventSrvc.getSignInSucceededEvent(), function() {
$scope.user = sessionSrvc.getCurrentUser();
$scope.signInVisible = false;
$scope.signOutVisible = true;
});
/**
* Show the SignIn dialog.
*/
$scope.signIn = function() {
$('#signIn').modal('show');
};
/**
* SignOut
*
* @param account
*/
$scope.signOut = function() {
var promise = sessionSrvc.signOut();
promise.then(function(user) {
$scope.signInVisible = true;
$scope.signOutVisible = false;
window.location.replace("/");
}, function(reason) {
// alert('Failed: ' + reason);
});
};
/**
* Detect if Cookies enabled
*/
checkCookie();
function checkCookie(){
var cookieEnabled=(navigator.cookieEnabled) ? true : false
if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){
document.cookie="testcookie";
cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false;
}
return (cookieEnabled) ? true:showCookieFail();
}
function showCookieFail(){
// alert('Please enable cookies, the site will not work correctly without them');
}
/**
* Set header back to portal header (from cookbook) if any tab is clicked
*/
$('body').on('click', '.nav li a', function(){
$('.header').removeClass('cookbook');
});
/**
* links to cookbook or other docs
*
* If user clicks back button or clicks any tab, changes header back to
* service portal (from cookbook).
*/
$scope.goHome = function() {
$('.header').removeClass('cookbook');
$state.go('home.explore');
};
window.onpopstate = function() {
$('.header').removeClass('cookbook');
};
}
)的控制器的簡單AngularJS單元測試;
這是我設置的通過測試的業績測試:)請在我擔任單元測試的角度對我來說是新的。我如何在我的測試中正確定義
'use strict'
describe('HeaderCtrl', function(){
var scope, sessionSrvc, eventSrvc, state;
beforeEach(module('mainApp'));
it('should have a HeaderCtrl controller', function() {
expect(mainApp.HeaderCtrl).toBe(undefined);
});
});
有意義 - 謝謝。但是我的測試仍然失敗並顯示一條消息:「未定義不是函數」 – ddeloy
堆棧跟蹤將有所幫助。它在哪裏失敗?也許這是由控制器本身造成的。在代碼中添加console.log()跟蹤。 –
非常感謝 - 我會試一試 – ddeloy