摩卡单元测试办公室插件

时间:2018-03-07 08:18:14

标签: unit-testing typescript mocha office-js office-addins

我尝试为outlook 365加载项编写单元测试。

到目前为止,我已使用ts-mockito模拟了许多课程。但是我遇到了一个问题,我目前无法轻易回避。执行测试时出现ReferenceError: Office is not defined错误。我将其跟踪到Office Enums的使用情况。

let item: MailItem;
// ...
let messageType = Office.MailboxEnums.ItemNotificationMessageType.ErrorMessage;
console.log('Won`t get here.');
item.AddNotification('Error404', { message: 'Element not found', type: messageType });

我嘲笑AddNotification方法,但我不能轻易地模仿Enum。我可以创建自己的Enum并在AddNotification方法上编写我的模型,并使用调用原始AddNotification方法的Office枚举替换我自己的枚举。但我不喜欢这种方式。

使用Office.debug.js在测试中包含--require也不起作用。

我还尝试在测试类或setup.js中定义枚举(mocha为--required)。

export namespace Office {
    export module MailboxEnums {
        export enum ItemNotificationMessageType {
            /**
             * The notificationMessage is a progress indicator.
             */
            ProgressIndicator,
            /**
             * The notificationMessage is an informational message.
             */
            InformationalMessage,
            /**
             * The notificationMessage is an error message.
             */
            ErrorMessage
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我需要将此枚举用于global,以便我可以随时使用它。

export namespace Office {
    export module MailboxEnums {
        export enum ItemNotificationMessageType {
            /**
             * The notificationMessage is a progress indicator.
             */
            ProgressIndicator,
            /**
             * The notificationMessage is an informational message.
             */
            InformationalMessage,
            /**
             * The notificationMessage is an error message.
             */
            ErrorMessage
        }
    }
}

// tslint:disable-next-line:no-any
(global as any).Office = Office;