【发布时间】:2018-11-21 11:16:57
【问题描述】:
我的 Angular 应用程序的单元测试存在一些问题,该应用程序旨在成为 Outlook 的插件。
代码本身运行良好,没有错误,但测试经常失败。
我得到的错误如下:
ReferenceError: Office is not defined
ReferenceError: fabric is not defined
测试在任何使用 Office.js 或 Fabric 的方法上都会失败。例如,如果一个方法如下:
public getName() {
this.item = Office.context.mailbox.item;
return this.item.from.displayName;
}
会因为没有定义office的错误而失败。
Office 和 Fabric 都通过 index.html 文件添加到应用程序中:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Firefish Outlook Addin</title>
<base href="/AddinClient/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://static2.sharepointonline.com/files/fabric/office-ui-fabric-js/1.4.0/css/fabric.min.css" />
<link rel="stylesheet" href="https://static2.sharepointonline.com/files/fabric/office-ui-fabric-js/1.4.0/css/fabric.components.min.css" />
<script src="assets/js/fabric.js"></script>
<script src="https://appsforoffice.microsoft.com/lib/1/hosted/Office.js" type="text/javascript"></script>
</head>
<body>
<app-root></app-root>
</body>
</html>
尤其是 office 在 main.ts 文件中初始化:
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './modules/app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
Office.initialize = function () {
platformBrowserDynamic()
.bootstrapModule(AppModule)
.catch(err => console.error(err));
};
我一直在网上寻找可能的解决方案,但遗憾的是找不到任何解决方案。我的 Karma.conf.js 文件如下:
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma')
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, '../coverage'),
reports: ['html', 'lcovonly'],
fixWebpackSourcePaths: true
},
Files: ["https://appsforoffice.microsoft.com/lib/1/hosted/Office.js"],
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
});
};
我的 tsconfig.spec.json 文件如下:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/spec",
"types": [
"jasmine",
"node",
"@types/office-js"
]
},
"files": [
"test.ts",
"polyfills.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts",
"../node_modules/office-ui-fabric-js/src/components/Dropdown/Dropdown.ts",
"../node_modules/office-ui-fabric-js/src/components/Button/Button.ts",
"../node_modules/office-ui-fabric-js/src/components/TextField/TextField.ts",
"../node_modules/office-ui-fabric-js/src/components/RadioButton/RadioButton.ts",
"../node_modules/office-ui-fabric-js/src/components/Spinner/Spinner.ts",
"../node_modules/office-ui-fabric-js/src/components/ProgressIndicator/ProgressIndicator.ts"
]
}
因此,我正在为 Office 和 fabric 文件添加必要的参考。
这是我需要创建 Office 和 fabric 模拟的案例吗?
【问题讨论】:
-
你必须在测试文件中导入这两种类型
-
在
tests.ts文件中,您应该添加您的依赖项。另外,在索引的头部使用脚本是一种不好的做法,您应该使用angular.json文件来导入JS脚本。
标签: angular typescript office-js office-ui-fabric