2015-05-19 29 views
0

我最近開始嘗試學習如何測試角度。我已經取得了一些成功,但目前我試圖測試一個指令,我似乎無法使其工作。在角度測試中運行示波器功能時出現問題

測試代碼

describe('navigation test', function(){ 
var element, location, route, rootScope, scope, httpBackend; 
var elm; 

beforeEach(module('myApp')); 

beforeEach(inject(function($compile,$rootScope,$location,$httpBackend,$route) { 


    location = $location; 
    scope = $rootScope.$new(); 

    route = $route; 
    $httpBackend.whenGET('partials/nav/nav.html') 
     .respond(200); 
    element = $compile("<navigation></navigation>")(scope); 
    httpBackend = $httpBackend; 
    scope.$digest(); 

})); 

describe('Change page function',function(){ 
    it('should flip the location between /home and /test', function(){ 
     location.path('/home') 
     scope.changePage(); 
     scope.$digest(); 
     expect(location.path()).to.equal('/test'); 
    }) 
}) 


}); 

指令功能

var app = angular.module('myApp'); 

app.directive('navigation', function($location, $q, $sce, $timeout, $mdSidenav, $mdComponentRegistry) { 
return { 
    restrict: 'E', 
    templateUrl: 'partials/nav/nav.html', 
    link: function(scope, elements, attrs) { 

     scope.changePage = function() { 
      if($location.path() == '/home') { 
       $location.path('/builder') 
      } else { 
       $location.path('/home'); 
      } 
     }; 

}); 我收到的錯誤是

錯誤信息

TypeError: 'undefined' is not a function

(評估 'scope.changePage()')

我想不通爲什麼它不能看到的範圍。任何幫助將不勝感激。如果任何人都可以在角度測試中發現一些很棒的東西。

+0

顯然你的指令不適用於範圍。請提供一個plunker/jsfiddle – Michael

+0

這是否不附加我的指示範圍? element = $ compile(「」)(scope);如果導航是我的指令。如果不是,我會如何去做呢? – BobDoleForPresident

+0

我不知道 - 我只看到你的指令的一部分.... /我沒有看到指令的名稱,也沒有看到它在哪個模塊中定義。 – Michael

回答

1

當您構建directive時,您忘記了致電$httpBackend.flush()partials/nav/nav.html提出請求。只需添加它,它應該工作正常。

Working fiddle