我很好奇窥探依赖关系的最好方法,所以我可以确保他们的方法在我的服务中被调用。我减少了我的代码,专注于手头的问题。我能够很好地测试我的服务,但我也希望能够确认我的服务(在这种情况下是metricService)具有也被调用的方法。我知道我必须以某种方式使用createSpyObj,但是当函数正确执行时,spyObj方法没有被捕获。我应该使用createSpyObj吗?或者我应该使用spyObj?当涉及依赖性时,我对间谍的概念感到困惑。
更新:当使用SpyOn我可以看到一个方法被调用,但其他方法不是
Test.spec
describe("Catalogs service", function() {
beforeEach(angular.mock.module("photonServicesCommons"));
var utilityService, metricsService, loggerService, catalogService, localStorageService;
var $httpBackend, $q, $scope;
beforeEach(
inject(function(
_catalogService_,
_metricsService_,
_$rootScope_,
_$httpBackend_
) {
catalogService = _catalogService_;
$scope = _$rootScope_.$new();
$httpBackend = _$httpBackend_;
$httpBackend.when('GET', "/ctrl/catalog/all-apps").respond(
{
catalogs: catalogs2
}
);
metricsService = _metricsService_;
startScope = spyOn(metricsService, 'startScope')
emitSuccess = spyOn(metricsService, 'emitGetCatalogSuccess').and.callThrough();
endScope = spyOn(metricsService, 'endScope');
})
);
afterEach(function(){
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
describe('get catalog', function(){
it("Should get catalogs", function(done) {
catalogService.normalizedDynamicAppList = testDynamicAppList1;
catalogService.response = null;
var promise3 = catalogService.getCatalog();
promise3.then(function (res) {
expect(res.catalogs).toEqual(catalogs2);
});
expect(metricsService.startScope).toHaveBeenCalled();
expect(metricsService.emitGetCatalogSuccess).toHaveBeenCalled();
expect(metricsService.endScope).toHaveBeenCalled();
$scope.$digest();
done();
$httpBackend.flush();
});
});
});
服务
public getCatalog(): IPromise<Interfaces.CatalogsResponse> {
if (this.response !== null) {
let finalResponse:any = angular.copy(this.response);
return this.$q.when(finalResponse);
}
return this.$q((resolve, reject) => {
this.metricsService.startScope(Constants.Photon.METRICS_GET_CATALOG_TIME);
this.$http.get(this.catalogEndpoint).then( (response) => {
let data: Interfaces.CatalogsResponse = response.data;
let catalogs = data.catalogs;
if (typeof(catalogs)) { // truthy check
catalogs.forEach((catalog: ICatalog) => {
catalog.applications.forEach((application: IPhotonApplication) => {
if( !application.appId ) {
application.appId = this.utilityService.generateUUID();
}
})
});
} else {
this.loggerService.error(this.TAG, "Got an empty catalog.");
}
this.response = data;
this.metricsService.emitGetCatalogSuccess();
console.log("CALLING END SCOPE");
this.metricsService.endScope(Constants.Photon.METRICS_GET_CATALOG_TIME);
resolve(finalResponse);
}).catch((data) => {
this.loggerService.error(this.TAG, "Error getting apps: " + data);
this.response = null;
this.metricsService.emitGetCatalogFailure();
reject(data);
});
});
} // end of getCatalog()
您可以使用spyOn而不是使用createSpyObj。如:
beforeEach(
inject(function(
_catalogService_,
_$rootScope_,
_$httpBackend_,
_metricsService_ //get the dependecy from the injector & then spy on it's properties
) {
catalogService = _catalogService_;
metricsService = _metricsService_;
$scope = _$rootScope_.$new();
...
// create the spy object for easy referral later on
someMethodSpy = jasmine.spyOn(metricsService, "someMethodIWannaSpyOn")
})
);
describe('get catalog', function(){
it("Should get catalogs", function(done) {
catalogService.normalizedDynamicAppList = testDynamicAppList1;
catalogService.response = null;
var promise3 = catalogService.getCatalog();
...other expects
...
//do the spy-related expectations on the function spy object
$httpBackend.flush(); // this causes the $http.get() to "move forward"
// and execution moves into the .then callback of the request.
expect(someMethodSpy).toHaveBeenCalled();
});
});
我在测试复杂的角度应用程序时使用了这种模式,并在角度服务包装器中包含外部导入/全局依赖项,以允许间谍和模拟它们进行测试。
createSpyObject在这里不起作用的原因是使用它将创建一个名为metricService的全新对象,并指定了间谍道具。它将不会被注入角度注入器测试的服务中的“metricService”。您希望从进样器获取实际相同的单件服务对象,然后监视它的属性。
另一个功能障碍的来源是$httpBackend.flush()
s位置。 $httpBackend
是$ http服务的模拟:您预先定义了您正在测试的代码所产生的任意数量的预期HTTP请求。然后,当您调用内部使用$ http的函数向某个url发出请求时,$ httpBackend会拦截对$ http方法的调用(并且可以执行诸如验证请求有效负载和标头以及响应之类的操作)。只有在测试代码调用$http
之后才调用$httpBackend.flush()
调用的then / error处理程序。这允许您进行必要的任何设置以准备一些测试状态,然后才触发.then
处理程序并继续执行异步逻辑。
对我个人而言,每次用$ httpBackend编写测试时都会发生同样的事情,并且总是需要一段时间来弄清楚或记住:)