2014-04-18 5 views
1

수 시간 동안 검색하고 실험 한 후 내 마음이 날아갔습니다. AngularJS 제공자를 단위 테스트하는 방법에 대한 직접적이고 실제적인 예는 어디에도 없습니다. 아무리 노력해도 문제가 해결되지 않습니다.AngularJS : 제공자를 단위 테스트하는 방법

아래 예에서 서비스가 제대로 주입되고 작동합니다. 공급자는 구성이 가능해야하는 코드 블록에 도착하지 않기 때문에 삽입 할 수 없으며 구성 할 수 없습니다.

'AngularJS 제공자를 단위 테스트하는 방법'을 제공 할 수있는 사람이 있습니까?

angular.module('app',[]); 
angular.module('app').service('testBasicService', function() { 
    return { 
     serviceMethod: function() { 
      alert("serviceMethod"); 
     } 
    }; 
}); 

angular.module('app').provider('testAdvancedService', function() { 
    this.$get = function() { 
     this.providerMethod = function() { 
      alert("providerMethod"); 
     } 
    } 
}); 


describe("Test", function() { 
    beforeEach(module("app"), function (testAdvancedServiceProvider) { 
     // code never arrives here 
    }); 

    it("should just work", inject(function (testBasicService,  testAdvancedService) { 
     testBasicService.serviceMethod(); 
     testAdvancedService.providerMethod(); // testAdvancedService is undefined here 
    })); 
}); 

답변

1

나는 공급자에서 $ GET 메소드가 리턴 값이 없기 때문에 testAdvancedService이 테스트에 정의되어 있지 않습니다 이유는 생각합니다. 객체를 반환하면 해당 객체는 테스트에 삽입 된 testAdvancedService가됩니다. 은 $ get 메소드의

describe("Test", function() { 

    beforeEach(module("app")); 

    var provider; 
    beforeEach(function() { 
     module(function ($provide, testAdvancedServiceProvider) { 
      provider = $provide.provider('testAdvancedServiceProvider', testAdvancedServiceProvider); 
     }); 
    }); 

    it("should just work", inject(function (testBasicService, testAdvancedService) { 
     testBasicService.serviceMethod(); 
     alert(testAdvancedService); // result of the $get function. undefined as it is now. 
     provider.providerMethod(); 
    })); 
}); 
+0

반환이 속임수를 썼는지 : 당신이 기능을 테스트 할 공급자에 정의되어있는 경우

, 나는이 작품 것을 발견했다. $ provide.provider 호출이 필요하지 않은 것 같습니다. 이것없이 모든 작품. 감사 ! –

관련 문제