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)
然而,没有什么是失败的,一切都显示为通过。当发生上述错误时,如何强制它失败?
Return 承诺测试结果如下。
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",
);
我正在尝试用 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)
然而,没有什么是失败的,一切都显示为通过。当发生上述错误时,如何强制它失败?
Return 承诺测试结果如下。
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",
);