[英]Using Jasmine to spy on document.createElement throws error from angular mock
I have an Angular service that handles the webcam.我有一个处理网络摄像头的 Angular 服务。 Here is the function I'm trying to test:这是我要测试的功能:
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);
};
I'm trying to mock the call to document.createElement and return a fake canvas object.我试图模拟对 document.createElement 的调用并返回一个假的画布对象。 Here is my test:这是我的测试:
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();
});
Here is the error I'm getting:这是我得到的错误:
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)
Does anyone have any idea why this is happening?有谁知道为什么会这样? I was looking in angular-mocks and it seems to be failing at this point:我正在查看 angular-mocks,但此时似乎失败了:
if (window.jasmine || window.mocha) {
...
if (injector) {
injector.get('$rootElement').off();
}
To the best of my knowledge, document.createElement is immutable.据我所知, document.createElement 是不可变的。 You really shouldn't be spying on it in the first place.你真的不应该首先监视它。 It's a built-in function that will perform the same way every time.这是一个内置函数,每次都以相同的方式执行。 Your other spies look fine.你的其他间谍看起来不错。
Its kind of weird to spy on that.监视它有点奇怪。 But -in greater versions of angular- you can accomplish it by just spying into the createElement
method.但是 - 在更大版本的 angular 中 - 你可以通过监视createElement
方法来完成它。 You can join this with a returnValue
with a jasmine spy to expect certain calls.您可以使用带有茉莉花间谍的returnValue
加入此操作,以期待某些调用。 Like this:像这样:
const toDataUrlSpy = jasmine.createSpy('dataUrl');
spyOn(document, 'createElement').and.returnValue({
width: 0,
height: '',
getContext: () => ({ drawImage: () => {} })
toDataURL: toDataUrlSpy
});
service.takePicture();
expect(toDataUrlSpy).toHaveBeenCalledTimes(1);
Cheers!干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.