诗农单元测试。如何对一个将返回一个承诺的函数进行单元测试,该承诺将通过回调调用另一个函数?

时间:2021-03-19 04:21:45

标签: javascript unit-testing testing sinon ava

我是诗浓的新手,但我已经环顾四周,试图找到这个问题的答案..

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

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 方法没有返回任何它返回回调(res);

我已尝试存根 bookService.InfoRequest 方法,但由于它没有返回任何内容,我不确定如何修改回调参数以测试所有 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,它也会通过...

1 个答案:

答案 0 :(得分:1)

.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/stackoverflow/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             
----------------|---------|----------|---------|---------|-------------------