Creando pruebas unitarias
Clase 7 de 19 • Curso de Arquitectura de Aplicaciones Descentralizadas en Ethereum
Contenido del curso
Clase 7 de 19 • Curso de Arquitectura de Aplicaciones Descentralizadas en Ethereum
Contenido del curso
Jesus Marquez Martini
Mateo Santiago Zapata Maldonado
Marcelo Haro
si estan realizando este curso despues del octuber 2 puede que les genere un error al intentar realizar el test, el error que tenia yo era "TypeError: platziFood.deployed is not a function" y era por que la nuevas versiones de hardhat no necesitan la funcion deployed(); yo simplemente al borrar esa linea el codigo me funciono como en la clase
const { ethers } = require("hardhat"); const { expect } = require("chai"); describe("PlatziFood", () => { const setup = async () => { const accounts = await ethers.getSigners(); const PlatziFood = await ethers.getContractFactory("PlatziFood"); const deployed = await PlatziFood.deploy(); return { accounts, deployed, }; }; describe("PlatziFood Contract", () => { it("getAllPlatziFoods return an empty PlatziFoodItem[]", async () => { const { deployed } = await setup(); expect(await deployed.getAllPlatziFoods()).to.eql([]); }); it("addPlatziPunk added correctly and array in platziFoods", async () => { const { deployed, accounts } = await setup(); const signer = accounts[0].address; const foodUrl = "test"; const foodName = "Pollito"; const originCountry = "CO"; await deployed.addPlatziFood(foodUrl, foodName, originCountry); expect(await deployed.getAllPlatziFoods()).to.eql([ [signer, foodUrl, foodName, originCountry], ]); }); describe("getPlatziFoodByOwner", async () => { it("should return all platzi food by owner", async () => { const { deployed } = await setup(); const foodUrl = "test"; const foodName = "Pollito"; const originCountry = "CO"; await deployed.addPlatziFood(foodUrl, foodName, originCountry); // ============================== const foodUrl2 = "test"; const foodName2 = "Carnita"; const originCountry2 = "VE"; await deployed.addPlatziFood(foodUrl2, foodName2, originCountry2); // ============================== const getAllFoodsByMainSigner = await deployed.getPlatziFoodsByOwner(); expect(getAllFoodsByMainSigner.length).to.equal(2); }); it("should return all platzi food by another address", async () => { const { accounts, deployed } = await setup(); const signer = accounts[1]; const foodUrl = "test"; const foodName = "Carnita"; const originCountry = "VE"; await deployed .connect(signer) .addPlatziFood(foodUrl, foodName, originCountry); expect( await deployed.connect(signer).getPlatziFoodsByOwner() ).to.have.lengthOf(1); }); }); }); });
Las pruebas son muy importantes ya que ayudan a detectar errores y así evitar re trabajos que consumen tiempo y a diferencia de web2 también conllevan costos económicos altos.