0
我有一個處理網絡攝像機的Angular服務。下面是我想要測試功能:使用茉莉來窺探document.createElement從角度模擬中拋出錯誤
this.takePicture = function() {
var canvas = document.createElement('canvas');
canvas.width = this.width;
canvas.height = this.height;
var context = canvas.getContext('2d');
context.drawImage(this.videoElement, 0, 0, this.width, this.height);
return canvas.toDataURL('image/jpeg', 100);
};
我試圖嘲弄調用使用document.createElement並返回一個假的畫布對象。下面是我的測試:
it('should draw an image', function() {
var drawImageSpy = jasmine.createSpy('drawImage');
var canvas = {
getContext: jasmine.createSpy('getContext').and.returnValue({ drawImage: drawImageSpy }),
width: 0,
height: 0,
toDataURL: jasmine.createSpy('toDataUrl').and.returnValue('data-uri')
};
document.createElement = jasmine.createSpy('createCanvas').and.returnValue(canvas);
WcCameraService.takePicture();
expect(drawImageSpy).toHaveBeenCalled();
});
這是我得到的錯誤:
TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
at TypeError (native)
at Function.jQuery.extend.buildFragment (C:/Projects/Accurev/WebCOE_FRF_DEV/src/bower_components/jquery/dist/jquery.js:5565:24)
at Function.jQuery.parseHTML (C:/Projects/Accurev/WebCOE_FRF_DEV/src/bower_components/jquery/dist/jquery.js:9923:18)
at jQuery.fn.init (C:/Projects/Accurev/WebCOE_FRF_DEV/src/bower_components/jquery/dist/jquery.js:2774:33)
at Object.jQuery [as element] (C:/Projects/Accurev/WebCOE_FRF_DEV/src/bower_components/jquery/dist/jquery.js:73:10)
at $get (C:/Projects/Accurev/WebCOE_FRF_DEV/src/bower_components/angular-mocks/angular-mocks.min.js:6:18224)
at Object.e [as invoke] (C:/Projects/Accurev/WebCOE_FRF_DEV/src/bower_components/angular/angular.min.js:39:193)
at C:/Projects/Accurev/WebCOE_FRF_DEV/src/bower_components/angular/angular.min.js:41:10
at Object.d [as get] (C:/Projects/Accurev/WebCOE_FRF_DEV/src/bower_components/angular/angular.min.js:38:394)
at Object.<anonymous> (C:/Projects/Accurev/WebCOE_FRF_DEV/src/bower_components/angular-mocks/angular-mocks.min.js:6:21105)
任何人都不會有爲什麼發生這種情況的任何想法?我正在尋找角模擬,它似乎在這一點上失敗:
if (window.jasmine || window.mocha) {
...
if (injector) {
injector.get('$rootElement').off();
}