ReferenceError: confirm is not defined
ReferenceError: confirm is not defined
我正在 运行使用 mocha 对函数进行单元测试。我看到这个错误 ReferenceError: confirm is not defined。我该如何解决?
file.js
function writeTOFile(server, pin) {
let dContinue = confirm("You are uploading a file . Do you want to continue?");
if(dContinue) {
//do something
} else {
....
}
}
**test.spec.js
let file = require("file.js");
const expect = require('chai').expect;
it('test something',function (){
file.writeTOFile("fghsssbn", "5647");
expect(somevalue).to.be(something);
})
当我运行 mocha test.js时,我看到上面的,如何通过这个错误。
这里是node
测试环境下的单元测试方案。您应该在 global
变量中添加 confirm
方法。
file.js
:
function writeTOFile(server, pin) {
let dContinue = confirm("You are uploading a file . Do you want to continue?");
if (dContinue) {
console.log("do something");
} else {
console.log("do another thing");
}
}
module.exports = { writeTOFile };
file.test.js
:
const sinon = require("sinon");
const file = require("./file");
describe("59883330", () => {
afterEach(() => {
sinon.restore();
});
it("should do something", function() {
global.confirm = sinon.stub().returns(true);
sinon.stub(console, "log");
file.writeTOFile("fghsssbn", "5647");
sinon.assert.calledWithExactly(global.confirm, "You are uploading a file . Do you want to continue?");
sinon.assert.calledWithExactly(console.log, "do something");
});
it("should do another thing", () => {
global.confirm = sinon.stub().returns(false);
sinon.stub(console, "log");
file.writeTOFile("fghsssbn", "5647");
sinon.assert.calledWithExactly(global.confirm, "You are uploading a file . Do you want to continue?");
sinon.assert.calledWithExactly(console.log, "do another thing");
});
});
100% 覆盖率的单元测试结果:
59883330
✓ should do something
✓ should do another thing
2 passing (11ms)
--------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
--------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
file.js | 100 | 100 | 100 | 100 | |
file.test.js | 100 | 100 | 100 | 100 | |
--------------|----------|----------|----------|----------|-------------------|
如果你的测试环境是browser
,confirm
方法存在于window
变量中。
源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/Whosebug/59883330
我正在 运行使用 mocha 对函数进行单元测试。我看到这个错误 ReferenceError: confirm is not defined。我该如何解决?
file.js
function writeTOFile(server, pin) {
let dContinue = confirm("You are uploading a file . Do you want to continue?");
if(dContinue) {
//do something
} else {
....
}
}
**test.spec.js
let file = require("file.js");
const expect = require('chai').expect;
it('test something',function (){
file.writeTOFile("fghsssbn", "5647");
expect(somevalue).to.be(something);
})
当我运行 mocha test.js时,我看到上面的,如何通过这个错误。
这里是node
测试环境下的单元测试方案。您应该在 global
变量中添加 confirm
方法。
file.js
:
function writeTOFile(server, pin) {
let dContinue = confirm("You are uploading a file . Do you want to continue?");
if (dContinue) {
console.log("do something");
} else {
console.log("do another thing");
}
}
module.exports = { writeTOFile };
file.test.js
:
const sinon = require("sinon");
const file = require("./file");
describe("59883330", () => {
afterEach(() => {
sinon.restore();
});
it("should do something", function() {
global.confirm = sinon.stub().returns(true);
sinon.stub(console, "log");
file.writeTOFile("fghsssbn", "5647");
sinon.assert.calledWithExactly(global.confirm, "You are uploading a file . Do you want to continue?");
sinon.assert.calledWithExactly(console.log, "do something");
});
it("should do another thing", () => {
global.confirm = sinon.stub().returns(false);
sinon.stub(console, "log");
file.writeTOFile("fghsssbn", "5647");
sinon.assert.calledWithExactly(global.confirm, "You are uploading a file . Do you want to continue?");
sinon.assert.calledWithExactly(console.log, "do another thing");
});
});
100% 覆盖率的单元测试结果:
59883330
✓ should do something
✓ should do another thing
2 passing (11ms)
--------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
--------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
file.js | 100 | 100 | 100 | 100 | |
file.test.js | 100 | 100 | 100 | 100 | |
--------------|----------|----------|----------|----------|-------------------|
如果你的测试环境是browser
,confirm
方法存在于window
变量中。
源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/Whosebug/59883330