我们如何断言嵌入在 HTML 中的 javascript 变量在使用电子构建的应用程序中具有某些预期值?当前的测试框架是 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
});
})