我有一项服务可以发布关于创建的帖子,如下所示:
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()
在服务创建中调用 to,这个错误就会消失,我可以测试我的控制器。
我的 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() {
}
});