没有exe文件的Spectron和Electron
Spectron and electron without exe files
我正在尝试使用 Electron 构建应用程序。
我需要基于电子环境并使用电子包进行一些单元测试。
这样,我正在使用 spectron 来模拟我的应用程序。
在文档中,写着我必须在 'path' 属性 中输入我的可执行文件所在的路径。我现在没有可执行文件,我处于开发模式。
这是我根据另一个问题尝试过的方法:
beforeEach(() => {
app = new Application({
path: 'node_modules/.bin/electron'
});
app.start().then(res => console.log(res), err => console.log(err));
});
提示中没有任何内容出现,以下测试失败表明我无法在未定义的对象上获取 getWindowCount(显然,应用程序未实例化):
it('should call currentWindow', (done) => {
app.client.getWindowCount().then((count) => {
expect(count).to.equals(1);
done();
});
});
有谁知道我应该在这条路径中放置什么才能使我的测试环境正常工作?
PS : 我正在使用摩卡柴和 sinon。
感谢您的帮助
起初我创建一个可执行文件是为了测试,但实际上没有必要。
您可以看到 Spectron 有一个 example test and a global setup。
该示例传递了一个名为 args 的选项,而这正是您所缺少的。这就是我正在做的事情:
var appPath = path.resolve(__dirname, '../'); //require the whole thing
var electronPath = path.resolve(__dirname, '../node_modules/.bin/electron');
beforeEach(function() {
myApp = new Application({
path: electronPath,
args: [appPath], // pass args along with path
});
return myApp.start().then(function() {
assert.equal(myApp.isRunning(), true);
chaiAsPromised.transferPromiseness = myApp.transferPromiseness;
return myApp;
});
});
我的测试位于 ./tests/app-test.js 中。以上对我有用。
如果您使用 doc 中提到的电子预建,您还可以向变量路径提供 "electron" :
path - Required. String path to the Electron application executable to
launch. Note: If you want to invoke electron directly with your app's
main script then you should specify path as electron via
electron-prebuilt and specify your app's main script path as the first
argument in the args array.
我认为它看起来像这样:
import electron from 'electron'
import { Application } from 'spectron'
describe('application launch', function () {
this.timeout(10000)
beforeEach(function () {
this.app = new Application({
path: electron,
args: ['app']
})
return this.app.start()
})
...
}
我正在尝试使用 Electron 构建应用程序。
我需要基于电子环境并使用电子包进行一些单元测试。
这样,我正在使用 spectron 来模拟我的应用程序。
在文档中,写着我必须在 'path' 属性 中输入我的可执行文件所在的路径。我现在没有可执行文件,我处于开发模式。
这是我根据另一个问题尝试过的方法:
beforeEach(() => {
app = new Application({
path: 'node_modules/.bin/electron'
});
app.start().then(res => console.log(res), err => console.log(err));
});
提示中没有任何内容出现,以下测试失败表明我无法在未定义的对象上获取 getWindowCount(显然,应用程序未实例化):
it('should call currentWindow', (done) => {
app.client.getWindowCount().then((count) => {
expect(count).to.equals(1);
done();
});
});
有谁知道我应该在这条路径中放置什么才能使我的测试环境正常工作?
PS : 我正在使用摩卡柴和 sinon。
感谢您的帮助
起初我创建一个可执行文件是为了测试,但实际上没有必要。
您可以看到 Spectron 有一个 example test and a global setup。
该示例传递了一个名为 args 的选项,而这正是您所缺少的。这就是我正在做的事情:
var appPath = path.resolve(__dirname, '../'); //require the whole thing
var electronPath = path.resolve(__dirname, '../node_modules/.bin/electron');
beforeEach(function() {
myApp = new Application({
path: electronPath,
args: [appPath], // pass args along with path
});
return myApp.start().then(function() {
assert.equal(myApp.isRunning(), true);
chaiAsPromised.transferPromiseness = myApp.transferPromiseness;
return myApp;
});
});
我的测试位于 ./tests/app-test.js 中。以上对我有用。
如果您使用 doc 中提到的电子预建,您还可以向变量路径提供 "electron" :
path - Required. String path to the Electron application executable to launch. Note: If you want to invoke electron directly with your app's main script then you should specify path as electron via electron-prebuilt and specify your app's main script path as the first argument in the args array.
我认为它看起来像这样:
import electron from 'electron'
import { Application } from 'spectron'
describe('application launch', function () {
this.timeout(10000)
beforeEach(function () {
this.app = new Application({
path: electron,
args: ['app']
})
return this.app.start()
})
...
}