Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在stubing:AssertionError:expected';一些数据';等于';短截线';_Javascript_Node.js_Unit Testing_Mocha.js_Sinon - Fatal编程技术网

Javascript 在stubing:AssertionError:expected';一些数据';等于';短截线';

Javascript 在stubing:AssertionError:expected';一些数据';等于';短截线';,javascript,node.js,unit-testing,mocha.js,sinon,Javascript,Node.js,Unit Testing,Mocha.js,Sinon,我不熟悉使用mocha和sinon进行单元测试,只是为了了解更多信息,我在代码中插入了一个函数以返回一些字符串,我将此函数称为内部函数。我的测试是查看存根返回字符串是否分配给变量。请查看代码片段以了解更多信息 file.specjs let sinon = require("sinon"); let filejs = require('./file.js'); let expect = require ("chai").expect; it('should run only the outer

我不熟悉使用mocha和sinon进行单元测试,只是为了了解更多信息,我在代码中插入了一个函数以返回一些字符串,我将此函数称为内部函数。我的测试是查看存根返回字符串是否分配给变量。请查看代码片段以了解更多信息

file.specjs

let sinon = require("sinon");
let filejs = require('./file.js');
let expect = require ("chai").expect;

it('should run only the outer function' ,function() {

// I try to stub my function here
sinon.stub(filejs,'test1').callsFake ((someArg) => {
  return "stubbed string";
});

// Now I will call my test outer function
   filejs.test();
  expect(filejs.param).to.equal("stubbed string");

})

由于我已经对函数test1进行了存根,所以我不希望调用它,并且test1的return被分配给param,因为我们伪造了函数以返回不同的字符串,所以我希望这个字符串被设置为param变量

但是当我运行测试时,我看到了这个错误

AssertionError:预期“某些数据”等于“存根字符串”


请尝试以下编辑

功能测试(){
module.exports.param=module.exports.test1();
}
为了有机会工作,你正在努力做什么。您需要
sinon
来修改
模块。导出
,被测代码需要从该对象读取
test1()。它可能需要嵌套得更深才能修改它。。。我不知道。我在使用sinon.stub(require('./something')时遇到问题。

我想我是在一个repl.it中工作的

对不起,我忘记添加规范文件:)使用
module.exports
而不是
module.export
谢谢Doug Coburn:)我还修改了我的代码段,我没有将任何参数传递给函数testI更改了我的问题,我现在看到断言错误,stubbed函数没有替换字符串值我回答得太快了。我的回答不正确。

    let param;
    module.exports = {
         test,
         test1
    }

    function test () {
      module.exports.param = test1();
    }

    function test1() {
       console.log("should not be called);
       let data = "some data";
       return data; 
    }