Unit testing 如何在angularjs中测试路由

Unit testing 如何在angularjs中测试路由,unit-testing,angularjs,routing,directive,karma-runner,Unit Testing,Angularjs,Routing,Directive,Karma Runner,在这里,我单击元素,并应将我带到ng ref指针,它可以工作 现在我编写一个单元测试: app.directive('ngRef', function($location, $timeout){ linkFn = function(sco,ele,att){ ele.bind('click', function(){ // console.log($location); $timeout(function(){

在这里,我单击元素,并应将我带到ng ref指针,它可以工作

现在我编写一个单元测试:

app.directive('ngRef', function($location, $timeout){
    linkFn = function(sco,ele,att){
        ele.bind('click', function(){
//            console.log($location);
            $timeout(function(){
                $location.path(att.ngRef);
            }, 0);
        })
    }
    return linkFn;
})
description('routing',function(){
var mockLocation=null,mockRootScope
beforeach(inject(function()){
模块('应用程序')
注入(函数($location,$rootScope){
mockLocation=$location;
mockRootScope=$rootScope;
})
}))
它('should route well',function(){
var elem=angular.element('Click');
var scope=rootScope.$new();
expect(mockLocation.path).toBe('/home');//true
(要素)(范围);
元素[0]。单击();
作用域:$apply();
mockTimeout.flush();
expect(mockLocation.path()).toBe('/contact');//false
})
})

expect(mockLocation.path()).toBe(“/contact”)总是显示一个错误,预期“/home”为“/contact”,在这种情况下,如何更改$location的路径?在测试中?这是一个超时问题吗?

这是谷歌集团的一篇老帖子,可能值得一试。使用karma测试angularjs路线。是文档。我正在使用karma,但我无法获取location.path以正常工作。location.path中出现了什么问题?
describe('routing', function(){
    var mockLocation = null, mockRootScope
    beforeEach(inject(function(){
        module('app')
        inject(function($location, $rootScope){
            mockLocation = $location;
            mockRootScope = $rootScope;
        })
    }))

    it('should route well', function(){
        var elem = angular.element('<button ng-ref="/contact">Click</button>');
        var scope = rootScope.$new();
        expect(mockLocation.path).toBe('/home'); //true
        mockCompile(elem)(scope);
        elem[0].click();

        scope.$apply();
        mockTimeout.flush();
        expect(mockLocation.path()).toBe('/contact'); //false
    })
})