无法在角度测试中运行示波器功能

时间:2015-05-19 11:58:16

标签: angularjs unit-testing mocha karma-runner

我最近开始尝试学习如何测试角度。我已经取得了一些成功,但目前我正在尝试测试一个指令,而我似乎无法使其成功。

测试代码

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:&#39; undefined&#39;不是一个功能

(评估&#39; scope.changePage()&#39;)

我无法弄清楚为什么它看不到范围。任何帮助将不胜感激。如果有人能够对角度测试有所了解,那就太棒了。

1 个答案:

答案 0 :(得分:1)

在构建$httpBackend.flush()时,您忘记致电partials/nav/nav.htmldirective提出请求。只需添加它,它应该工作正常。

Working fiddle