0
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;
})

在这里,我单击元素,应该将我带到 ng-ref 指针,该指针有效。

现在我写一个单元测试:

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
    })
})

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

4

0 回答 0