gpt4 book ai didi

testing - Spectron、mocha 和 chai 能否断言变量在 Electron 应用程序中具有预期值?

转载 作者:行者123 更新时间:2023-11-28 20:27:36 24 4
gpt4 key购买 nike

我们如何断言嵌入在 HTML 中的 javascript 变量在使用 electron 构建的应用程序中具有一些预期值?目前测试框架有spectron、mocha、chai、chai.should()、chai.use(chaiAsPromised)。

我想断言全局变量 foo具有值 'foo' .当我尝试 foo.should.equal('foo')我得到 ReferenceError: foo is not defined at Context.<anonymous> (test\spec.js:63:28)

下面是重新设计的 spec.js。

const Application = require('spectron').Application
const assert = require('assert')
const electronPath = require('electron') // Require Electron from the binaries included in node_modules.
const path = require('path')
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const should = require('chai').should();

describe('Isolated testbeds house independent suites of tests.', function() {
this.timeout(30000);

before(function() {
this.app = new Application({
path: electronPath,

// directory structure:

// |__ myProject
// |__ ...
// |__ main.js
// |__ package.json
// |__ index.html
// |__ ...
// |__ test
// |__ spec.js <- You are here! ~ Well you should be.

args: [path.join(__dirname, '..')]
})
return this.app.start()
});

after(function() {
if (this.app && this.app.isRunning()) {
return this.app.stop()
}
});

/* BELOW IS THE TEST IN QUESTION */
it('should have a given value', function() {
return Promise.resolve(foo).should.eventually.equal('foo'); // HERE IS THE LINE IN QUESTION
});

})

最佳答案

Spectron 正在“远程控制”您的 Electron 应用程序,并且不在同一个命名空间中。这就是为什么 foo 没有在你的测试脚本中定义。

如果 foo 在您的 Electron 前端中,如果它在 DOM 中,您可以使用 this.app.client 访问它。 this.app.browserWindowthis.app.webContents 可能能够访问全局变量?

(我知道 executeJavaScript() 不会工作 - 任何返回 promise 的函数基本上都不会工作。)

如果 foo 在您的后端,我会在我的问题中展示一个解决方法:Can Spectron call a function in back-end directly? (但我仍在寻找一种不需要我修改代码即可测试的方法)

关于testing - Spectron、mocha 和 chai 能否断言变量在 Electron 应用程序中具有预期值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49041558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com