2015-05-19 90 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