網站搜尋

typescript - Chai 問題模擬 API 回應 - 函數未被 stub


我正在嘗試測試一個 API 回應。我不知道我模擬 req、res 的方式是否是最好的,但我的程式碼是:

it('should failed when missing auth header', async () => {
  const req = {} as functions.https.Request;
  const res: any = {
    status: function (statusCode: number) {
      assert.strictEqual(statusCode, 401);
      return this;
    },
    send: function (body: unknown) {
      assert.deepEqual(body, {
        error: "Unauthorized",
      });
    },
  };
  await createNudgeBankLinkingNotifications(req, res);
  sinon.assert.calledOnce(res.status);
});

但是當我運行測試時,我收到此錯誤

AssertError: function (statusCode) {
                chai_1.assert.equal(statusCode, 401);
                return this;
            } is not stubbed

正在測試的函數是:

export const createNudgeBankLinkingNotifications = 

functions.https.onRequest(async (req, res) => {
  if (!hasAuthHeaderAPIKey(req)) {
    res.status(401).send({
      error: 'Unauthorized'
    });
    return
  }
  const { correlationId } = createFunctionLogger(
    'createNudgeBankLinkingNotifications',
    fileLevelLogger,
  );
  try {
    await sendNudgeBankLinkingNotifications(correlationId);
    res.status(200).send({ success: true });
  } catch (error) {
    res.status(500).send({
      error: 'Notification cannot be sent'
    });
  }
});

有人知道怎麼修復這個嗎?