模拟函数后如何检查 属性 值:断言错误,mocha
How to check the property value after mocking a function : Assertion Error ,mocha
根据问题中的建议enter link description here 我模拟了 readFileSync 并模拟了我的外部函数,现在我想验证变量值是否按预期设置
file.js
const fs1 = require('fs');
let param;
module.export = {
test,
param
}
function test (outputParam) {
param = fs1.readFileSync(outputParam);
}
我已经存根了这个 readFileSync 并且它return指定了文件内容,如下面的测试所示
当我运行测试时我想看到变量param有文件内容值
test.spec.js
let expect = require('chai').expect;
let fs = require("fs");
let sinon = require("sinon");
let filejs = require('./file.js');
it('should run only the outer function and test if variable param is set to `this is my file content` ' ,function() {
let someArg ="file.xml";
sinon.stub(fs,'readFileSync').callsFake ((someArg) => {
return "this is my file content";
});
var mock = sinon.mock(filejs);
mock.expects('test').withArgs(someArg);
expect(filejs.param).to.equal('this is my file content');
})
来自 file.js,如您所见,属性 参数从 "readFileSync" 获取值,该值存根为 return 值
当我运行考试
expect(filejs.param).to.equal('this is my file content');
AssertionError: expected undefined to equal 'this is my file content'
注意 module.exports
的正确拼写 - 而不是 module.export
。
在您的 file.js 中,变量 param
未初始化,因此它将获得值 undefined
并且也将使用该值导出。如果您稍后修改 param
,导出的值不会改变。导出仅将 属性 绑定到值,而不是变量。事实上,您甚至不需要局部变量。要动态更改导出的值,至少在 Node 中,您只需在 module.exports
.
上重新分配 属性
const fs1 = require('fs');
module.exports = {
test
}
function test (outputParam) {
module.exports.param = fs1.readFileSync(outputParam);
}
至于您的 test.spec.js,您已经非常接近让它发挥作用了。将不应在集线器下调用的方法存根后,只需调用 test
函数,传递真实参数即可。那里不需要模拟。
let expect = require('chai').expect;
let fs = require("fs");
let sinon = require("sinon");
let filejs = require('./file.js');
it('should run only the outer function and test if variable param is set to `this is my file content` ' ,function() {
let someArg ="file.xml";
sinon.stub(fs,'readFileSync').callsFake ((someArg) => {
return "this is my file content";
});
filejs.test(someArg);
expect(filejs.param).to.equal('this is my file content');
});
根据问题中的建议enter link description here 我模拟了 readFileSync 并模拟了我的外部函数,现在我想验证变量值是否按预期设置
file.js
const fs1 = require('fs');
let param;
module.export = {
test,
param
}
function test (outputParam) {
param = fs1.readFileSync(outputParam);
}
我已经存根了这个 readFileSync 并且它return指定了文件内容,如下面的测试所示
当我运行测试时我想看到变量param有文件内容值
test.spec.js
let expect = require('chai').expect;
let fs = require("fs");
let sinon = require("sinon");
let filejs = require('./file.js');
it('should run only the outer function and test if variable param is set to `this is my file content` ' ,function() {
let someArg ="file.xml";
sinon.stub(fs,'readFileSync').callsFake ((someArg) => {
return "this is my file content";
});
var mock = sinon.mock(filejs);
mock.expects('test').withArgs(someArg);
expect(filejs.param).to.equal('this is my file content');
})
来自 file.js,如您所见,属性 参数从 "readFileSync" 获取值,该值存根为 return 值
当我运行考试
expect(filejs.param).to.equal('this is my file content');
AssertionError: expected undefined to equal 'this is my file content'
注意 module.exports
的正确拼写 - 而不是 module.export
。
在您的 file.js 中,变量 param
未初始化,因此它将获得值 undefined
并且也将使用该值导出。如果您稍后修改 param
,导出的值不会改变。导出仅将 属性 绑定到值,而不是变量。事实上,您甚至不需要局部变量。要动态更改导出的值,至少在 Node 中,您只需在 module.exports
.
const fs1 = require('fs');
module.exports = {
test
}
function test (outputParam) {
module.exports.param = fs1.readFileSync(outputParam);
}
至于您的 test.spec.js,您已经非常接近让它发挥作用了。将不应在集线器下调用的方法存根后,只需调用 test
函数,传递真实参数即可。那里不需要模拟。
let expect = require('chai').expect;
let fs = require("fs");
let sinon = require("sinon");
let filejs = require('./file.js');
it('should run only the outer function and test if variable param is set to `this is my file content` ' ,function() {
let someArg ="file.xml";
sinon.stub(fs,'readFileSync').callsFake ((someArg) => {
return "this is my file content";
});
filejs.test(someArg);
expect(filejs.param).to.equal('this is my file content');
});