Sinon 单元测试。如何对一个函数进行单元测试,该函数将 return 承诺将调用另一个带回调函数的函数?

Sinon unit testing. How to unit test a function that will return a promise that will call another function with callback?

我是 Sinon 的新手,但我已经四处寻找了一段时间,试图找到这个问题的答案..

我有一个函数需要测试,它 returns 承诺调用另一个函数将回调.. 以下是我需要为其编写测试用例的函数:

const bookService = require(./bookService);

const getBook = () => {
  const bookName = "book";
  const bookID = '111';
  return new Promise((resolve, reject) => {
    bookService.InfoRequest(bookName, bookID, 'GET', res => {
      if(res.error){
         reject(res);
      }else{
         const list = res['allPages'] || [];
         if(list = []){
           resolve({
             pageNumber: 0,
             note: "book is no longer exist"
           });
         }else{
             resolve(res['allPages']);
         }  
      }
    })
  })
} 

bookService.InfoRequest 方法没有返回任何东西 returns 回调 (res);

我已尝试对 bookService.InfoRequest 方法进行 stub,但由于它没有返回任何内容,我不确定如何修改回调参数以测试所有 3 个分支..

我正在使用 Ava,所以我尝试了这样的操作:

test('getBook Error Block', t=> {
    const stub = sinon.stub(bookService, InfoRequest);
    stub.callsFake(() => {
    return { error: true };
    });
    
    return obj.getBook().then(res => {
    t.deepEqual(res, []);
}).catch(error => {
    console.log(error.error);
    t.deepEqual(error.error, true);
})

})

这是第一个分支 reject(res) 分支的测试用例。还有2个非常相似只是callFake不同。

但问题是我无法将错误打印出来,测试显示它通过了,但是如果我将 true 更改为 false,它也通过了...

.callFake() 的存根实现不正确。 bookService.InfoRequest() 方法接受一个回调参数,res 被传递给这个回调。因此,您需要使用此 callback 函数提供存根实现并传递您的假错误。

例如

bookService.js:

function InfoRequest(bookName, bookId, method, cb) {}

module.exports = { InfoRequest };

obj.js:

const bookService = require('./bookService');

const getBook = () => {
  const bookName = 'book';
  const bookID = '111';
  return new Promise((resolve, reject) => {
    bookService.InfoRequest(bookName, bookID, 'GET', (res) => {
      if (res.error) {
        reject(res);
      } else {
        const list = res['allPages'] || [];
        if ((list = [])) {
          resolve({
            pageNumber: 0,
            note: 'book is no longer exist',
          });
        } else {
          resolve(res['allPages']);
        }
      }
    });
  });
};

module.exports = { getBook };

obj.test.js:

const obj = require('./obj');
const bookService = require('./bookService');
const sinon = require('sinon');
const test = require('ava');

test('getBook Error Block', (t) => {
  const res = { error: new Error('network') };
  const stub = sinon.stub(bookService, 'InfoRequest').callsFake((bookName, bookId, method, callback) => {
    callback(res);
  });

  return obj.getBook().catch((res) => {
    t.deepEqual(res.error, res.error);
    sinon.assert.calledWith(stub, 'book', '111', 'GET', sinon.match.func);
  });
});

测试结果:

> nyc ava --timeout=3000 "/Users/dulin/workspace/github.com/mrdulin/expressjs-research/src/Whosebug/66702460/obj.test.js"


  1 test passed
----------------|---------|----------|---------|---------|-------------------
File            | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------------|---------|----------|---------|---------|-------------------
All files       |   71.43 |    16.67 |      75 |   71.43 |                   
 bookService.js |     100 |      100 |       0 |     100 |                   
 obj.js         |   69.23 |    16.67 |     100 |   69.23 | 11-18             
----------------|---------|----------|---------|---------|-------------------