最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sinon stubbing giving 'is not a function' error - Stack Overflow

programmeradmin0浏览0评论

first time really using sinon and I am having some issues with the mocking library.

All I am trying to do is stub/mock out a function from a dao class called myMethod. Unfortunatly, I am getting the error: myMethod is not a function, which makes me believe I am either putting the await/async keywords in the wrong spots of the test or I don't understand sinon stubbing 100%. Here is the code:

// index.js
async function doWork(sqlDao, task, from, to) {
  ...
  results = await sqlDao.myMethod(from, to);
  ...
}

module.exports = {
  _doWork: doWork,
  TASK_NAME: TASK_NAME
};
// index.test.js

const chai = require("chai");
const expect = chai.expect;
const sinon = require("sinon");

const { _doWork, TASK_NAME } = require("./index.js");
const SqlDao = require("./sqlDao.js");

.
.
.

  it("given access_request task then return valid results", async () => {
    const sqlDao = new SqlDao(1, 2, 3, 4);
    const stub = sinon
      .stub(sqlDao, "myMethod")
      .withArgs(sinon.match.any, sinon.match.any)
      .resolves([{ x: 1 }, { x: 2 }]);

    const result = await _doWork(stub, TASK_NAME, new Date(), new Date());
    console.log(result);
  });

With error:

  1) doWork
       given task_name task then return valid results:
     TypeError: sqlDao.myMethod is not a function

first time really using sinon and I am having some issues with the mocking library.

All I am trying to do is stub/mock out a function from a dao class called myMethod. Unfortunatly, I am getting the error: myMethod is not a function, which makes me believe I am either putting the await/async keywords in the wrong spots of the test or I don't understand sinon stubbing 100%. Here is the code:

// index.js
async function doWork(sqlDao, task, from, to) {
  ...
  results = await sqlDao.myMethod(from, to);
  ...
}

module.exports = {
  _doWork: doWork,
  TASK_NAME: TASK_NAME
};
// index.test.js

const chai = require("chai");
const expect = chai.expect;
const sinon = require("sinon");

const { _doWork, TASK_NAME } = require("./index.js");
const SqlDao = require("./sqlDao.js");

.
.
.

  it("given access_request task then return valid results", async () => {
    const sqlDao = new SqlDao(1, 2, 3, 4);
    const stub = sinon
      .stub(sqlDao, "myMethod")
      .withArgs(sinon.match.any, sinon.match.any)
      .resolves([{ x: 1 }, { x: 2 }]);

    const result = await _doWork(stub, TASK_NAME, new Date(), new Date());
    console.log(result);
  });

With error:

  1) doWork
       given task_name task then return valid results:
     TypeError: sqlDao.myMethod is not a function

Share Improve this question edited Feb 24, 2019 at 18:25 Jay asked Feb 24, 2019 at 18:06 JayJay 2,6861 gold badge17 silver badges24 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

Your issue is that you're passing stub to _doWork instead of passing sqlDao.

A stub isn't the object that you just stubbed. It is still a sinon object that you use to define the behaviour of the stubbed method. When you're done with your tests, you use stub to restore stubbed object.

const theAnswer = {
    give: () => 42
};

const stub = sinon.stub(theAnswer, 'give').returns('forty two');

// stubbed
console.log(theAnswer.give());

// restored 
stub.restore();
console.log(theAnswer.give());
<script src="https://cdnjs.cloudflare./ajax/libs/sinon.js/7.2.4/sinon.min.js"></script>

发布评论

评论列表(0)

  1. 暂无评论