【问题标题】:Sinon stub is not working in this scenario?Sinon stub 在这种情况下不工作?
【发布时间】:2018-02-17 15:14:51
【问题描述】:

我正在尝试使用 sinon stub 来测试我的函数,方法是为 IF 语句创建模拟值,正如 testFunction 中描述的那样工作

在 myFunction.js 文件之一中,我有类似的功能

function testFunction() {
  var job = this.win.get.value1   //test
  var job1 = this.win.get.value2 // test1
  if(job === 'test' && job1 === 'test1') {
    return true;
  }
    return false; 
}

我正在尝试使用 karma 测试 testFunction,我尝试使用这样的 sinon 存根来存根测试函数

it('should test my function', function(done) {
  var stub = sinon.stub(myFunction,'job','job1').returns('test','test1');
  myFunction.testFunction('test', function(err, decodedPayload) {
    decodedPayload.should.equal(true);
    done();
  });
});

谁能告诉我哪里出错了?

【问题讨论】:

    标签: javascript unit-testing sinon stub


    【解决方案1】:

    您错误地使用了sinon.stub。您需要对 sinon.stub 进行两次调用,一次调用要存根的 myFunction 的每个方法。

    it('should test my function', function(done) {
      sinon.stub(myFunction,'job').returns('test');
      sinon.stub(myFunction,'job1').returns('test1');
      myFunction.testFunction('test', function(err, decodedPayload) {
        decodedPayload.should.equal(true);
        done();
      });
    });
    

    See Sinon documentation

    【讨论】:

    • 这两个不是函数,而是变量中的两个值,这是存根值的正确方法吗?
    • 上面的答案给出了一个错误“试图将作业的未定义属性包装为函数”
    猜你喜欢
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    • 2018-06-16
    • 2017-06-21
    相关资源
    最近更新 更多