2014-02-06 18 views
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); 
    }); 
    }); 

回答

2

當視圖或路徑需要時,控制器會根據需求進行實例化。所以,你的應用程序沒有你的控制器的任何實例,並且你的控制器不是像服務那樣​​的單例。所以你的測試沒有意義。你可以測試的是創建控制器實例的能力:

describe('HeaderCtrl', function(){ 
    var $controller, $scope; 
    beforeEach(module('mainApp')); 

    beforeEach(inject(function($rootScope, _$controller_) { 
     $scope = $rootScope.$new(); 
     $controller = _$controller_; 
    })); 

    it('should create a HeaderCtrl controller', function() { 
     var controller = $controller('HeaderCtrl', { 
      $scope: $scope 
     }); 
     expect(controller).toBeDefined(); 
    }); 
}); 
+0

有意義 - 謝謝。但是我的測試仍然失敗並顯示一條消息:「未定義不是函數」 – ddeloy

+0

堆棧跟蹤將有所幫助。它在哪裏失敗?也許這是由控制器本身造成的。在代碼中添加console.log()跟蹤。 –

+0

非常感謝 - 我會試一試 – ddeloy