我似乎无法弄清楚如何获得" - 生成POD返回PDF文件'规格。 Jasmine在任何返回之前超时,所以我无法测试。当我拿出完成回调函数然后测试运行正常但我怀疑它是误报。
控制器
(function() {
'use strict';
angular
.module('app.controllers')
.controller('podController', podController);
podController.$inject = ['podService','$window'];
function podController(podService,$window) {
var vm = this;
vm.generatePOD = generatePOD;
vm.downloadPDF = downloadPDF;
vm.openPDF = openPDF;
/////////////
function downloadPDF(willCallId){
var fileName = 'pod-' + willCallId;
generatePODFile(willCallId).then(function(file){
openPDF(file,fileName);
});
}
function generatePOD(willCallId) {
return podService.getPOD(willCallId).then(function(data){
var byteCharacters = atob(data);
var byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
var file = new Blob([byteArray], {type: 'application/pdf'});
return file;
});
}
function openPDF(file,fileName){
var fileURL = $window.URL.createObjectURL(file);
$window.open(fileURL,fileName);
}
}
})();
服务
(function() {
'use strict';
angular
.module('app.services')
.service('podService', podService);
podService.$inject = ['IEHRestangular'];
function podService(IEHRestangular) {
var service = {
getPOD: getPOD
};
return service;
///////////////////////
function getPOD(willCallId) {
return IEHRestangular.one('/ClientEventNotification/api/POD/').customGET('GetPOD', {'willcallid': willCallId},{responseType: 'arraybuffer'}).then(function (data) {
return data;
});
}
}
})();
控制器测试套件
describe('POD Controller Test', function () {
beforeEach(module('app'));
beforeEach(module(function($urlRouterProvider) {
$urlRouterProvider.deferIntercept();
}));
var $controller,$window,podService,$q,vm,deferred;
beforeEach(inject(function (_$controller_,$window,_podService_,$q,$timeout){
deferred = $q.defer();
$controller = _$controller_;
$window = $window;
$timeout = $timeout;
podservice = _podService_;
$q = $q;
vm = $controller('podController', {'$window': $window, podservice:podService});
}));
it(' - Generate POD returns a PDF File', function (done) {
var willcallid = 3265987;
deferred.resolve('resolveData');
spyOn(podservice, 'getPOD').and.returnValues(deferred.promise);
vm.generatePOD(willcallid).then(function(data){
expect(data).toBe((data instanceof Blob));
expect(podservice.getPOD).toHaveBeenCalledWith(willcallid);
done();
});
});
it(' - Opens a window displaying a PDF File', inject( function( $window ) {
spyOn( $window, 'open' ).and.callFake( function() {
return true;
} );
var byteArray = new Uint8Array(1024);
var file = new Blob([byteArray], {type: 'application/pdf'});
vm.openPDF(file,'testFile');
expect( $window.open ).toHaveBeenCalled();
} ) );
});
答案 0 :(得分:0)
使用ngMock
时,许多服务被可以以同步方式控制的版本替换,以便在测试期间更好地控制流。这意味着在许多情况下您不需要使用Jasmine done
回调。
知道承诺与摘要周期相关也很重要,这意味着承诺的then
,catch
和finally
回调仅在摘要运行后执行。
您不需要进行太多更改:
it(' - Generate POD returns a PDF File', function() {
var willcallid = 3265987;
spyOn(podservice, 'getPOD').and.returnValue(deferred.promise);
vm.generatePOD(willcallid).then(function (data) {
expect(podservice.getPOD).toHaveBeenCalledWith(willcallid);
expect(data instanceof Blob).toBe(true);
});
deferred.resolve('resolveData');
// Trigger the digest loop and resolution of promise callbacks
$rootScope.$digest();
});
在您的示例中删除done
回调时发生的事情是规范将在没有任何等待的情况下运行,但是promise回调函数永远不会被执行,期望永远不会被执行并且测试将通过(因为在承诺回调之外没有exepcts)。