簡體   English   中英

chai 和 sinon 測試不會失敗,但應該

[英]chai and sinon test does not fail but it should

我正在嘗試使用 chai、sinon 和 chai promise 編寫單元測試。 這是我的代碼

import chai, { expect } from "chai";
import chaiAsPromised from "chai-as-promised";
import dotenv from "dotenv";
import { request, response } from "express";

import { describe, it } from "mocha";
import sinon from "sinon";


    it("test", () => {
  sandbox.stub(resultsDatastore, "search").resolves({
    rows: [
      {
        fields: {
          "network_traffic.dst_ref.resolves_to_refs.value":
            "00:00:00:00:00:00",
          "user_account.is_privileged": "false",
          "x_com_ibm_ariel.category_id": "8052",
        },
      },
    ],
  });
  resultService
    .extractIndexesFromRow("test", { query: {} })
    .should.eventually.deep.include(
      "111network_traffic.dst_ref.resolves_to_refs.value",
    );

運行測試后,在我的控制台中,我可以看到測試評估日志:

    (node:68149) UnhandledPromiseRejectionWarning: AssertionError: expected 'network_traffic.dst_ref.resolves_to_refs.value' to deep include '111network_traffic.dst_ref.resolves_to_refs.value'
    at /Users/Hamed.Minaee@ibm.com/Desktop/IBM/temp/investigate-backend/tests/unit-tests/api/v1/services/ResultService.test.ts:1345:48
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:68149) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 22)
(node:68149) UnhandledPromiseRejectionWarning: AssertionError: object tested must be an array, a map, an object, a set, a string, or a weakset, but object given
(node:68149) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 23)

然而,沒有任何失敗,一切都顯示為通過。 發生上述錯誤時,如何強制它失敗?

返回測試結果的承諾,如下所示。

it("test", () => {
  sandbox.stub(resultsDatastore, "search").resolves({
    rows: [
      {
        fields: {
          "network_traffic.dst_ref.resolves_to_refs.value":
            "00:00:00:00:00:00",
          "user_account.is_privileged": "false",
          "x_com_ibm_ariel.category_id": "8052",
        },
      },
    ],
  });
  return resultService
    .extractIndexesFromRow("test", { query: {} })
    .should.eventually.deep.include(
      "111network_traffic.dst_ref.resolves_to_refs.value",
    );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM