我试图为我的电子js应用程序编写单元测试,并且我被困在一个地方。
例如
让我们说我的应用代码如下
2
现在,如果我想在电子客户端上运行它,它将在客户端PC上安装电子模块时完美运行
问题在于我正在尝试为上述单元测试用例编写单元测试用例,因为电子没有安装在dev机器上,如下所示
var app= (function () {
function app() {
var _this = this;
this.open = function (data) {
var deferred = Q.defer();
try {
// some code
}
catch (ex) {
deferred.reject(ex);
}
return deferred.promise;
};
}
}());
exports.app = app
获得以下错误
import { app} from '../../app'
import { Rights } from '../../Rights'
describe('app', () => {
let app: app;
beforeEach(() => {
app= new app();
})
it('should call open() and return error for null content', function (done) {
let output = app.open(null);
output
.then((res) => {
//expectation
done();
})
.catch((err)=>{
//expectation
done();
})
})
})
问题
答案 0 :(得分:0)
您可以通过覆盖require
:
module._load
来电
const m = require('module');
const originalLoader = m._load;
const stubs = { electron : {} };
m._load = function hookedLoader(request, parent, isMain) {
const stub = stubs[request];
return stub || originalLoader(request, parent, isMain);
};
答案 1 :(得分:0)
我遇到过与mocha类似的问题。我的基本方法是: *挂钩前需要电子, *用本地默认值覆盖它 *需要应用程序 *挂钩后清理
以下是一个示例:
var el = require('../../modules/electron');
describe('app', function () {
'use strict';
var el_override = {
post: function () {
},
get: function () {
}
}, app;
before(function () {
/* Since we already have electron required, we override it
from node cache by:
require.cache['/path/to/file/name'].exports = el_override
*/
// Require app
// This will import overridden electron
App = require('app-location');
});
beforeEach(function () {
// init app
app = new App();
});
after(function () {
// Clean Up both electron override and app by
// delete requre.cache['/path/to/file/names']
});
it('should test for batch', function () {
// Call you functions
});
});