【发布时间】:2018-12-15 04:45:28
【问题描述】:
我是 mocha/chai/testing 的新手,如果这是一个愚蠢的问题,请原谅我,但我不知道如何解决这个问题。
我的代码(一个 Word 插件)本身工作得很好,但是当我尝试对其运行 mocha 测试时,它在几行中到达了这个初始化函数:
Office.initialize = (reason) => {
$('#sideload-msg').hide();
$('#app-body').show();
};
然后停止运行,吐出这个错误:
ReferenceError: Office is not defined
早些时候它吐出同样的东西,但对于 XMLHttpRequest (ReferenceError: XMLHttpRequest is not defined) 但我设法通过将它添加到页面顶部来解决它:
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
我会尝试为 Office 做同样的事情,但我什至不确定我要require 做什么,而且我在网上找不到任何信息。
如果有帮助,这就是我的 test.js 页面的全部内容:
const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('../src/index.js');
const should = chai.should();
const APIURL = "https://example.com";
chai.use(chaiHttp);
describe('API Calls', function() {
it('should return 200 status code on /api/context-key GET', function (done) {
chai.request(server)
.get(APIURL + '/api/context-key')
.end(function (err, res) {
res.should.have.status(200);
done();
})
});
});
我什至不确定我的结构是否正确?它使用我的加载项的 index.js 页面作为server,但我想要做的是测试具有 URL https://example.com/api/context-key 的 API 在发送 GET 请求时返回 200 状态代码。我走远了吗?就像我说的,我对测试完全陌生:/
编辑:实际上 XMLHttpRequest 修复使测试运行,但破坏了应用程序。
【问题讨论】:
-
我对测试不太了解,但是当 Office.js 不在 Office 应用程序的加载项中运行时,它无法初始化。我假设测试在 Office 应用程序之外运行。因此,即使您找到了引用和定义
Office的方法,它也不会初始化。您会在控制台中收到一条错误消息,指出 Office 未在 Office 主机中运行。
标签: javascript mocha.js office-js chai