Jasmine 当变量未定义时,是否有一种简单的方法来检查方法是否未被调用?
因此,我有一个简单的方法,仅当定义了传递的变量时才执行某些操作:Jasmine 当变量未定义时,是否有一种简单的方法来检查方法是否未被调用?,jasmine,Jasmine,因此,我有一个简单的方法,仅当定义了传递的变量时才执行某些操作: public myFunction(item) { if (typeof item !== 'undefined') { item.doSomething(); } } 这是我在《茉莉花》中的测试: describe('myFunction()', () => { it ('should only do something if the item passed is defi
public myFunction(item) {
if (typeof item !== 'undefined') {
item.doSomething();
}
}
这是我在《茉莉花》中的测试:
describe('myFunction()', () => {
it ('should only do something if the item passed is defined.', () => {
const item = new Item();
spyOn(item, 'doSomething');
service.myFunction(item);
//this works
expect(item.doSomething).toHaveBeenCalledTimes(1);
});
it ('should not do something if the item passed is undefined.', () => {
const item = undefined;
spyOn(item, 'doSomething');
service.myFunction(item);
//this does not work..
expect(item.doSomething).toHaveBeenCalledTimes(0);
});
});
我的第一次测试很好。但我不知道如何表达我的第二次测试。当传递的项未定义时,如何说从未调用过doSomething
?这看起来很琐碎,但我在这方面遇到了麻烦。我有一种感觉,这是不可能的,因为我无法监视未定义的东西。再说一次,也许有解决办法?试试:
it ('should not do something if the item passed is undefined.', () => {
const item = undefined;
const conditionForIf = typeof item !== 'undefined';
// check the conditionForIf, if it is false, it won't go on and `doSomething`
expect(conditionForIf).toBe(false);
});
当我试着那样做时,我犯了这个错误<代码>错误::找不到用于监视doSomething的对象是的,我怀疑发生了这种情况。我做了一个编辑,以不同的方式测试它。要测试它,你提出的方法是困难的,如果不是不可能的。