我正在angular
项目上工作,我需要进行审核。我有一个Web服务来进行插入,并在我的角度项目中调用它,我有一个审计服务,它将使用审计Web服务。该服务本身可以正常工作,它调用Web服务并插入行,但是当我尝试覆盖该服务时,我遇到了以下问题
(function () {
'use strict';
angular
.module('directContactInfo')
.factory('auditService', ['$http', 'serviceResolver',
auditServiceFactory
]);
function auditServiceFactory(http, serviceResolver) {
return {
performAudit: function (data, token) {
var postData = angular.toJson(data, true);
http({
method: "POST",
headers: {
'Authorization': 'Bearer ' + token
},
url:
serviceResolver.auditWebservice.endpoints.post,
data: postData
});
}
}
}
})();
我正在尝试使用以下内容来涵盖上述服务
'use strict';
describe('Services: auditService', function () {
beforeEach(module('directContactInfo'));
beforeEach(inject(function ($httpBackend) {
$httpBackend.whenGET('@@staticFilePath/customer-direct-contact-info-
search.html').respond(
200, '');
}));
afterEach(inject(function ($httpBackend) {
$httpBackend.flush();
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
}));
it('should only make a post call with the correct endpoint and key',
inject(function ($httpBackend, auditService, serviceResolver)
{
var accessToken = 'accessToken';
var customerIdUpdate = 1234555;
var customerDirectMessageAddressOldValue = "xyz@gmail.com";
var customerDirectMessageAddressNewValue = "abc@gmail.com";
var auditData = buildAccessAuditObj(customerIdUpdate,
customerDirectMessageAddressOldValue,
customerDirectMessageAddressNewValue);
var postData = angular.toJson(auditData, true);
// We only expect a GET request with the supplied data to be triggered.
$httpBackend.expectPOST(
serviceResolver.auditWebservice.endpoints.post, postData,
{
'Authorization': 'Bearer ' + accessToken,
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
}, undefined).respond(function () {
return [201,
// jscs:disable
{}
// jscs:enable
];
});
var criteria = {
prvarAudApplId: '1234',
prvarAuditActionId: '1234',
userIdentification: '1234',
profileName: 'ABCD',
prvarProxyUserId: '0',
prvarProxyProfileName: '',
remoteAddress: '127.0.0.0',
customerId: providerNpiUpdate,
additionalDataText1: customerDirectMessageAddressOldValue ,
additionalDataText2: customerDirectMessageAddressNewValue
};
// Make the call which relies on the mocked $httpBackend.
providerAccessAuditService.performAudit(criteria, accessToken);
}));
function buildAccessAuditObj(selectedCustId, oldDirectMessageAddress,
newDirectMessageAddress) {
return {
prvarAudApplId: '1234',
prvarAuditActionId: '1234',
userIdentification: '1234',
profileName: 'ABCD',
prvarProxyUserId: '0',
prvarProxyProfileName: '',
remoteAddress: '127.0.0.0',
customerNpi: selectedCustId,
additionalDataText1: oldDirectMessageAddress,
additionalDataText2: newDirectMessageAddress
};
}
});
而且它抛出的异常是
Error: [$injector:unpr] Unknown provider: AuditServiceProvider
<- auditService
https://errors.angularjs.org/1.7.2/$injector/unpr?
p0=auditServiceProvider%20%3C-%20auditService
at bower_components/angular/angular.js:138:12
at bower_components/angular/angular.js:4891:19
at Object.getService [as get] (bower_components/angular/angular.js:5051:32)
at bower_components/angular/angular.js:4896:45
at getService (bower_components/angular/angular.js:5051:32)
at injectionArgs (bower_components/angular/angular.js:5076:58)
at Object.invoke (bower_components/angular/angular.js:5100:18)
at Object.WorkFn (bower_components/angular-mocks/angular-mocks.js:3264:20)
Error: Declaration Location
at window.inject.angular.mock.inject (bower_components/angular-
mocks/angular-mocks.js:3227:25)
at Suite.<anonymous> (app/services/auditService.spec.js:18:72)
at app/services/auditService.spec.js:3:1
答案 0 :(得分:0)
确保您的karma.conf.js加载了定义要尝试注入的服务的脚本,并确保您的模块具有将该服务定义为依赖项的模块(基本上1.x具有一个注入器用于应用程序的生命周期,用于使实例从任何模块或其从属模块中注入。