簡體   English   中英

Jasmine Class 外部 Function 的單元測試不起作用

[英]Jasmine Unit Test for Function outside Class not working

I was trying to test a static function within a logo-manager.component.ts file but the function is outside of "export class LogoManager"

export class LogoManagerComponent implements OnInit {
...
}

function dataURLtoFile(dataurl, filename) {
...
}

我嘗試在 logo-manager.component.spec.ts 中進行導入,例如

import * as LogoManagerObj from './logo-manager.component';

然后

const fileObject = getFakeEventData({name: 'name', type: 'image/png', size: 500});
spyOn(LogoManagerObj, 'dataURLtoFile').and.returnValue(fileObject);

但我收到錯誤““dataURLtoFile”類型的參數不能分配給“LogoManagerComponent”類型的參數。

除了嘗試使用 spyOn 之外,我還嘗試使用 spyOnProperty 但沒有奏效。

提前感謝您的評論。

dataURLtoFileLogoManagerObj中不存在。

嘗試以下操作:

export class LogoManagerComponent implements OnInit {
...
}

export function dataURLtoFile(dataurl, filename) { // add export here 
// so we can have a handle on it in the tests
...
}
import * as LogoManagerObj from './logo-manager.component';

.....

const fileObject = getFakeEventData({name: 'name', type: 'image/png', size: 500});
spyOn(LogoManagerObj, 'dataURLtoFile').and.returnValue(fileObject);
// to get a handle on the component, it would be LogoManagerObj.LogoManagerComponent

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM