模拟获取帖子

问题描述 投票:0回答:1

我正在为以下功能编写测试用例:

handleShare(response, payload) {
    ShareUtils.listUsers()
      .then((users) => {
          users.forEach(user => userList.push({
              name: user.name,
              id: user.id)
          });
          return SessionUtils.getSessionData(SESSION_ID));
      })
  .then((sessionData) => {
    //construct share object
    return fetch('http://myEndpoint', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    })
  })
  .then(r => r.json())
  .then((response) => {
    Logger.log(response)
  })
  .catch((err) => {
    ErrorHandler.error(err)
  });
}

我的测试用例是

describe('Share Controller', function() {
  it('should call handleShare correctly', function(done) {
    let listUsers = sinon.stub(ShareUtils, 'listUsers').resolves(listOfUsers);
    let sessionData = sinon.stub(SessionUtils, 'getSessionData').resolves(slackSessionData);
    fetchMock.mock({
        name: 'share',
        matcher: '/myEndpoint/',
        method: 'POST',
        response: 'ok'
      })
      .catch();
    ShareController.handleShare({}, payload);
    console.log(fetchMock.done());
    expect(fetchMock.called()).to.be.true;
    done();
  });
});

但是每当我运行测试用例时,它都会输出 断言错误:预期 false 为 true

我错过了什么吗?我已经正确设置了摩卡测试套件,并且所有其他测试都运行良好,但是这个 fetch 模拟确实很麻烦。 感谢您提前的帮助。

node.js unit-testing mocha.js integration-testing fetch-mock
1个回答
0
投票

我在嘲笑时缺少内容类型标题!

© www.soinside.com 2019 - 2024. All rights reserved.