Para que sea más explícito saber cómo escribir pruebas para mi código existente, mantengo un código no tan bueno. Como probar esto...
const saveProducts = () => { arr = [ { name: "Mini Tiffin", category: "Kitchen", description: "kadai", price: 399, }, { name: "Big Boy Burger", category: "American", description: "Double patty burger with cheese and fries", price: 500, }, ]; arr.forEach(function (item, req, res) { productImg = saveProductImg(); return Product.findOne({ name: item.name }, (err, data) => { if (!data) { try { const newProd = new Product({ name: item.name, description: item.description, price: item.price, category: item.category, productImg: productImg, }); newProd.save(); } catch (err) { console.log(err); } } }); }); }; esto esta fallando (saveProducts es module.export ed)
it("Save should be called two times", async (done) => { // Arrange const arrMock = [{name: 'one'}, {name: 'two'}]; const revert = productController.__set__("arr", arrMock); const findOneMock = sinon.stub(Product, "findOne").resolves(undefined); const saveMock = sinon.stub(Product.prototype, "save"); // findOneMock.returns(sinon.promise().resolves(undefined)); await productController.saveProducts(); expect(saveMock).to.have.been.called; done(); });