次のような作成に関する投稿を行うサービスがあります。
angular.module('app', ['$strap.directives'])
.service('dataService', function ($rootScope, $http, $location) {
this.postInitCommands = function (path, command) {
$http.post(path,
command,
{headers:{'Content-Type':'application/x-www-form-urlencoded'}
}).success(function (data, status, headers, config) {
console.dir(data);
});
};
this.postInitCommands("/example/path", {example: 'command'});
}
ただし、このモジュールとサービスを使用するコントローラーのいくつかをテストしようとすると、それらはすべてエラーですぐに失敗しましUnexpected request: POST /example/path
た。
サービスの作成で呼び出しを行うとpostInitCommands()
、このエラーはなくなり、コントローラーをテストできます。
私のqunitの設定は次のとおりです。
var fittingDataServiceMock, injector, ctrl, $scope, $httpBackend;
module("Fitting Controller Test", {
setup: function() {
injector = angular.injector(['ngMock', 'ng','app']);
$scope = injector.get('$rootScope').$new();
dataServiceMock= injector.get('dataService');
$httpBackend = injector.get('$httpBackend');
$httpBackend.whenPOST('/example/test').respond(200, {});
ctrl = injector.get('$controller')(DoubleVariableController, { $scope: $scope, dataService: dataServiceMock});
},
teardown: function() {
}
});