13

내가 $scope에서 값을 가져옵니다과 다른 상태로 전송하는 컨트롤러가를 AngularJS와. go 메서드가 호출되었는지 확인하거나 Karma/AngularJS를 사용하여 when($state.go('search', $stateParams)).then(called = true);을 수행 할 수 있습니까?테스트는 UI 라우터의 이동() 메소드

답변

31

이 두 가지 모두 재스민 스파이와 할 수있는 것처럼 들립니다.

describe('my unit tests', function() { 
    beforeEach(inject(function($state) { 
     spyOn($state, 'go'); 
     // or 
     spyOn($state, 'go').andCallFake(function(state, params) { 
      // This replaces the 'go' functionality for the duration of your test 
     }); 
    })); 

    it('should test something', inject(function($state){ 
     // Call something that eventually hits $state.go 
     expect($state.go).toHaveBeenCalled(); 
     expect($state.go).toHaveBeenCalledWith(expectedState, expectedParams); 
     // ... 
    })); 
}); 

여기에 좋은 스파이 쪽지 http://tobyho.com/2011/12/15/jasmine-spy-cheatsheet/ 또는 실제 재스민 문서 here 있습니다.

스파이 사용에 대한 좋은 점은 명시 적으로 말하지 않는 한 실제로 상태 변환을 수행하지 않아도된다는 것입니다. Karma에서 URL을 변경하면 상태 전환이 실패합니다.

+0

완벽하고 방금 찾고 있던 것이 었습니다. – shmish111

+8

Jasmine 2.x에서는 함수 호출'.andCallFake'를'.and.callFake'로 바꿉니다. – satJ

관련 문제