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指針,它的工作原理。如何在angularjs中測試路由
現在我寫一個單元測試:
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( '/接觸')總是顯示預計 '/家' 的錯誤是「/接觸',在這種情況下如何更改$ location的路徑?在測試裏面?這是超時問題嗎?
這是谷歌集團的一箇舊帖子,https://groups.google.com/d/msg/angular/F0jFWC4G9hI/FNz3oQu0RhYJ值得一試。 – ivarni