代码之家  ›  专栏  ›  技术社区  ›  lokimidgard

带Mocha的单元测试办公室插件

  •  0
  • lokimidgard  · 技术社区  · 6 年前

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

    到目前为止,我已经用 ts-mockito 。然而,我面临着一个目前无法轻易回避的问题。我得到一个 ReferenceError: Office is not defined 执行测试时出错。我追踪到了Office枚举的使用情况。

    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 方法,但我无法轻松地模拟枚举。我可以创建自己的枚举并在 添加通知 方法,并在内部用调用原始 添加通知 方法但我不喜欢这一套。

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

    我还尝试在测试类或设置中定义枚举。js(即 --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 回复  |  直到 6 年前
        1
  •  1
  •   lokimidgard    6 年前

    我需要将此枚举分配给 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;