私は、 '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」サービスをモックアップする必要があるということですか?
ありがとうございました!
'ajax'は嘲笑されるべきではありません。期待どおりに動作するはずです。 'ajax'を注入するという事実は、別の場所に注入されているか、' planListingService'が偽装されていないという2つのことから生じます。あなたのテストにはいくつかの問題があるかもしれません。明らかに、 'module( 'myApp')'は 'beforeEach(module( 'myApp'))'でなければなりません。 – estus