我在Sinon一直在捣乱和嘲弄服务,但是当服务返回$ resources时,它从来没有对我有用。我需要一些帮助!
这些服务(首先返回$资源):
terminalService.factory('PrinterData', [ '$resource', function($resource) {
return $resource('ws/Admin/printer/data');
} ]);
terminalService.factory('PrinterService', ['$http', function ($http) {
return {
test:function() {
$http({url: 'ws/Admin/printer/test',
method: 'POST', headers: {'Content-Type': 'application/x-www-form-urlencoded'}}
).then(function () {
return;
});
},
replacePaper : function() {
$http({url: 'ws/Admin/printer/replacePaper',
method: 'POST', headers: {'Content-Type': 'application/x-www-form-urlencoded'}}
).then(function () {
return;
});
},
setPrinter : function(jsonData) {
var req = {
method: 'POST',
url: 'ws/Admin/printer/set',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
params: jsonData,
};
$http(req).success(function(){
});
}
}
}]);
这是我使用服务的控制器:
terminalController.controller('PrinterTestController', ['$rootScope', '$scope', 'PrinterData', 'PrinterService',
function($rootScope, $scope, PrinterData, PrinterService) {
$rootScope.currentPage = 'PRINTER_TEST';
PrinterData.get().$promise.then(function (response) {
$scope.printerData = response;
$scope.printers = response.printers;
$rootScope.ports = response.ports;
$rootScope.printerSettings = response.printerSettings;
});
$scope.testPrinter = function() {
PrinterService.test();
};
$scope.replacePaper= function() {
PrinterService.replacePaper();
};
$scope.setPrinter = function() {
var jsonData = {
settings: '{"model": "' + $rootScope.printerSettings.model + '","port":"'
+ $rootScope.printerSettings.port + '","askClientPrintReceipt":"'
+ $rootScope.printerSettings.askClientPrintReceipt + '"}'
};
PrinterService.setPrinter(jsonData);
};
}
]);
最后我的测试:
describe('PrinterTestController', function() {
var rootScope, scope, stubbedPrinterData, q;
beforeEach(module('eshtaPayTerminalApp.controllers'));
fakePrinterService = {
replacePaper : function() {},
test: function(){},
setPrinter : function(){}
};
beforeEach(inject(function($controller, $rootScope, $q, PrinterData) {
q = $q;
scope = $rootScope.$new();
rootScope = $rootScope.$new();
stubbedPrinterData = sinon.stub(PrinterData);
$controller('PrinterTestController', {
$rootScope : rootScope,
$scope : scope,
PrinterData : stubbedPrinterData,
PrinterService : fakePrinterService
});
}));
it('should not fail', function(){
expect(true).toBe(true);
});
});
返回$ resources的服务总是抛出: 错误:[$ injector:unpr] http://errors.angularjs.org/1.3.0/ $ injector / unpr?p0 = PrinterDataProvider%20%3C-%20PrinterData
有人可以帮忙吗?