使用 sinon 进行存根 fs.promises.readFile()

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

我在node.js中有这个fn,它从文件中读取json数据。

async getAllProducts() {
        try {
            return JSON.parse(await fs.promises.readFile("data/products.json"));
        } catch (error) {
            if (error.message === "Unexpected end of JSON input") {
                throw new NoProductsExistError("The File is Empty");
            }
            throw new FileReadingError("Error Reading File");
        }
    }

我正在尝试存根该方法。这是到目前为止我的代码。

const chai = require("chai");
const expect = chai.expect;
const sinon = require("sinon");
const dao = require("./dao");
const fs = require("fs");

describe("getAllProducts", () => {
    it("should return all products", () => {
        sinon
            .stub(fs.promises, "readFile")
            .withArgs("data/products.json")
            .returns(
                JSON.stringify([
                    {
                        productId: 101,
                        productName: "Sony XB450AP Wired Headset"
                    },
                    {
                        productId: 102,
                        productName: "Sony XB950"
                    }
                ])
            );

        expect(dao.getAllProducts()).to.equal([
            {
                productId: 101,
                productName: "Sony XB450AP Wired Headset"
            },
            {
                productId: 102,
                productName: "Sony XB950"
            }
        ]);
    });
});

但是,当我在配置了 mocha 的情况下运行 npm test 时,我得到了这个

 1) getAllProducts
       should return all products:
     AssertionError: expected {} to equal [ Array(2) ]
      at Context.<anonymous> (data\dao.spec.js:27:35)
      at processImmediate (internal/timers.js:458:21)

不知道如何解决这个问题。非常感谢任何帮助

javascript unit-testing chai sinon fs
2个回答
0
投票

NM,我明白了...

正确代码

const chai = require("chai");
const expect = chai.expect;
const sinon = require("sinon");
const dao = require("./dao");
const fs = require("fs");

describe("getAllProducts", () => {
    it("should return all products", *async* () => {
        sinon
            .stub(fs.promises, "readFile")
            .withArgs("data/products.json")
            .returns(
                JSON.stringify([
                    {
                        productId: 101,
                        productName: "Sony XB450AP Wired Headset"
                    },
                    {
                        productId: 102,
                        productName: "Sony XB950"
                    }
                ])
            );

        expect(*await* dao.getAllProducts()).to.*deep*.equal([
            {
                productId: 101,
                productName: "Sony XB450AP Wired Headset"
            },
            {
                productId: 102,
                productName: "Sony XB950"
            }
        ]);
    });
});

0
投票

您的函数 getAllProducts() 是一个 async 函数。所以你需要在函数之前放置await。 所以代码应该是

describe("getAllProducts", () => {
it("should return all products", *async* () => {
let fsStub = sinon
        .stub(fs.promises, "readFile")
        .withArgs("data/products.json")
        .returns(
            JSON.stringify([
                {
                    productId: 101,
                    productName: "Sony XB450AP Wired Headset"
                },
                {
                    productId: 102,
                    productName: "Sony XB950"
                }
            ])
        );
    await dao.getAllProducts();
    expect(fsStub).to.be.equal(JSON.stringify([
                    {
                        productId: 101,
                        productName: "Sony XB450AP Wired Headset"
                    },
                    {
                        productId: 102,
                        productName: "Sony XB950"
                    }
                ]));  
});

}); 我认为应该有效。

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