有没有一种简单的方法来检查未定义变量时是否未调用方法?

Is there a simple way for to check if method was not called when a variable is undefined?

所以我有一个简单的方法,仅当定义了传递的变量时才执行某些操作:

public myFunction(item) {
    if (typeof item !== 'undefined') {
        item.doSomething();
    }
}

这是我在 jasmine 中的测试:

    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 从未被调用过?这看起来很微不足道,但我遇到了麻烦。我觉得这是不可能的,因为我无法监视 undefined 的东西。再说一次,也许有解决办法?

尝试:

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);
    });