2016-08-05 11 views
0

私は、 'planListingService'というサービスに依存するディレクティブ( 'planListing')をテストしています。このサービスは 'ajax'と呼ばれる別のサービスに依存しています(悪名のためにメッセンジャーを撃ってはいけません)。jasmine mocking service inside service

ディレクティブをコンパイルしてスコープをロードし、コントローラをCAVEATで取得できます。アヤックス< - - planListingService ajaxProvider <:

エラー:[$インジェクター:UNPR]不明なプロバイダ私は両方のサービスのplanListingService」と「AJAX」を模擬することを余儀なくされています今のところそうでない私はこのようなエラーになります http://errors.angularjs.org/1.3.20/ $インジェクター/ UNPR?P0 = ajaxProvider%20%3C-%20ajax%20%3C-%20planListingService

私は 'planListingService' をモックアップされたので、私は実際には気にする必要はないということを考えましたこのサービスの実装や依存関係はありません。私はあまりにも多くを期待していますか?

planListing.js

angular.module('myApp') 
    .directive('planListing', planListing) 
    .controller('planListingCtrl', PlanListingCtrl); 

function planListing() { 
    var varDirective = { 
     restrict: 'E', 
     controller: PlanListingCtrl, 
     controllerAs: 'vm', 
     templateUrl: "scripts/directives/planListing/planListing.html"; 
     } 
    }; 
    return varDirective; 
} 
PlanListingCtrl.$inject = ['planListingService']; 
function PlanListingCtrl(planListingService) { 
    ... 
} 

planListingService.js

angular.module('myApp') 
    .factory('planListingService', planListingService); 
planListingService.$inject = ['$q', 'ajax']; 
function planListingService($q, ajax) { 
    ... 
} 

ajax.js

angular.module('myApp') 
    .factory('ajax', ['backend', '$browser', 'settings', '$http', '$log', 
    function (backend, $browser, settings, $http, $log) { 
    ... 

planListing.spec:ここ

一言でコードです。 js

describe('testing planListing.js',function(){ 

    var el,ctrl,scope,vm; 
    var service; 

    module('myApp'); 
    module('my.templates'); 

    beforeEach(module(function ($provide){ 
     // This seems to have no effect at all, why? 
     $provide.service('planListingService', function() { 
      this.getAllPricePlans=function(){}; 
     }); 
     // I don't get the error if I uncomment this: 
     // $provide.service('ajax', function ($q) { 
     // this.getAllPricePlans=function(){}; 
     // }); 
    })); 

    beforeEach(function() { 
     module('myApp'); 
     module('my.templates'); 
    }); 

    beforeEach(angular.mock.inject(function (_$compile_,_$rootScope_,_$controller_){ 
     $compile=_$compile_; 
     $rootScope = _$rootScope_; 
     $controller = _$controller_; 
     el = angular.element('<plan-listing></plan-listing>'); 
     scope = $rootScope.$new(); 
     $compile(el)(scope); 
     scope.$digest(); 

     ctrl = el.controller('planListing'); 
     scope = el.isolateScope() || el.scope(); 
     vm = scope.vm; 
    })); 

    describe('testing compilation/linking', function(){ 
     it('should have found directive and compiled template', function() { 
      expect(el).toBeDefined(); 
      expect(el.html()).not.toEqual(''); 
      expect(el.html()).toContain("plan-listing-section"); 
     }); 
    }); 
    it('should have a defined controller',function(){ 
     expect(ctrl).toBeDefined(); 
    }); 
    it('should have a defined scope',function(){ 
     expect(ctrl).toBeDefined(); 
    }); 
}); 

は、なぜ私は私は1つが「AJAX」サービスを呼び出している「planListingService」をモックアップしていていても「AJAX」サービスをモックアップする必要があるということですか?

ありがとうございました!

+1

'ajax'は嘲笑されるべきではありません。期待どおりに動作するはずです。 'ajax'を注入するという事実は、別の場所に注入されているか、' planListingService'が偽装されていないという2つのことから生じます。あなたのテストにはいくつかの問題があるかもしれません。明らかに、 'module( 'myApp')'は 'beforeEach(module( 'myApp'))'でなければなりません。 – estus

答えて

0

をテストする前にそれを行うことができ

をエラー与える注入されていない場合は、ここで(これに気付いに感謝の@estus)答えがあります:

問題は私のモジュールの間違った初期化に関連していました。この代わりに:予想通り物事が再び仕事を始めた後

describe('testing planListing.js',function(){ 

    var el,ctrl,scope,vm; 
    var service; 

    beforeEach(module('myApp')); 
    beforeEach(module('my.templates')); 
    ... 

describe('testing planListing.js',function(){ 

    var el,ctrl,scope,vm; 
    var service; 

    module('myApp'); 
    module('my.templates'); 
    ... 

私はこれをやったはずです。

1

私はそこにいました...悪いスタートのように感じます。あなたの指示はサービスに依存していると思います。指示に従うには注入する必要があります。指示を呼ぶだけで、それは意味しませんそれはあなたのテストでそれを注入するつもりです。それはそれを探しますし、それはそれはあなたが文書化のために、あなたのディレクティブ

beforeEach(inject(function ($injector) { 
yourService = $injector.get('yourService'); 
}) 
+0

実際、注入されることを意味します。指令コントローラは 'planListingService'に依存し、インスタンスを注入します。 – estus