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
})
})
期望(mockLocation.path())。toBe('/ contact')总是显示预期'/ home'的错误为'/ contact',如何在这种情况下更改$ location的路径?在测试里面?这是暂停问题吗?