2013-10-23 33 views
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指針,它的工作原理。如何在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的路徑?在測試裏面?這是超時問題嗎?

+0

這是谷歌集團的一箇舊帖子,https://groups.google.com/d/msg/angular/F0jFWC4G9hI/FNz3oQu0RhYJ值得一試。 – ivarni

回答

1

使用karma來測試angularjs路由。 Here是文檔。

+0

我使用業力,但我不能讓location.path正常工作。 – user2167582

+0

你在location.path中遇到了什麼問題? –