Jasmine-如何对使用静态方法导入另一个类的TypeScript类进行单元测试
我的Ionic应用程序中有一个简单的TypeScript类,它实现了一个简单的类型化字典Jasmine-如何对使用静态方法导入另一个类的TypeScript类进行单元测试,typescript,ionic2,jasmine,karma-jasmine,Typescript,Ionic2,Jasmine,Karma Jasmine,我的Ionic应用程序中有一个简单的TypeScript类,它实现了一个简单的类型化字典 import { Utils } from './utils'; export class Dictionary<T> { constructor(private noCase?: boolean, init?: Array<{ key: string; value:T; }>) { .... } } 我在Utils的Dictionary类中使用的唯一东西是
import { Utils } from './utils';
export class Dictionary<T> {
constructor(private noCase?: boolean, init?: Array<{ key: string; value:T; }>) {
....
}
}
我在Utils的Dictionary类中使用的唯一东西是上面所示的静态保护方法
有没有一种方法可以用静态方法测试包含这个其他类的类?我可以模拟这个类的静态方法吗
虽然上面的类很简单,但我还想测试其他东西,包括这个静态Utils类
spyOn(Utils, 'guard').and.returnValue(true);
提前感谢您的建议。是的,您可以添加一个间谍来模拟Utils类中的guard方法
spyOn(Utils, 'guard').and.returnValue(true);
谢谢你。我的实际问题是有点循环引用,我在导入的静态类中引用了测试中的类,即我从'./Dictionary'导入了{Dictionary};在Utils类中
import * as moment from 'moment';
import 'moment-duration-format';
import * as _ from 'lodash';
import { TranslateService } from 'ng2-translate';
import { Dictionary } from './dictionary';
.....
export class Utils {
public static guard(obj: any, name: string): void {
if (obj == null || obj == undefined)
throw (name + " must not be null!");
}
public static guardS(s: string, name: string): void {
if (this.isNullorEmptyOrWhiteSpace(s))
throw (name + " must not be null or empty!");
}
... etc
}
spyOn(Utils, 'guard').and.returnValue(true);