1
const { assert, expect } = require("chai");
const { getNamedAccounts, ethers, network } = require("hardhat");
const { developmentChains } = require("../../helper-hardhat-config");

developmentChains.includes(network.name)
  ? describe.skip
  : describe("Raffle Staging Tests", function () {
      let raffle, raffleEntranceFee, deployer;

      beforeEach(async function () {
        deployer = (await getNamedAccounts()).deployer;
        raffle = await ethers.getContract("Raffle", deployer);
        raffleEntranceFee = await raffle.getEntranceFee();
      });

      describe("fulfillRandomWords", function () {
        it("works with live Chainlink Keepers and Chainlink VRF, we get a random winner", async function () {
          // enter the raffle
          console.log("Setting up test...");
          const startingTimeStamp = await raffle.getLastTimeStamp();
          const accounts = await ethers.getSigners();

          console.log("Setting up Listener...");
          await new Promise(async (resolve, reject) => {
            // setup listener before we enter the raffle
            // Just in case the blockchain moves REALLY fast
            raffle.once("WinnerPicked", async () => {
              console.log("WinnerPicked event fired!");
              try {
                // add our asserts here
                const recentWinner = await raffle.getRecentWinner();
                const raffleState = await raffle.getRaffleState();
                const winnerEndingBalance = await accounts[0].getBalance();
                const endingTimeStamp = await raffle.getLastTimeStamp();

                await expect(raffle.getPlayer(0)).to.be.reverted;
                assert.equal(recentWinner.toString(), accounts[0].address);
                assert.equal(raffleState, 0);
                assert.equal(
                  winnerEndingBalance.toString(),
                  winnerStartingBalance.add(raffleEntranceFee).toString()
                );
                assert(endingTimeStamp > startingTimeStamp);
                resolve();
              } catch (error) {
                console.log(error);
                reject(error);
              }
            });
            // Then entering the raffle
            console.log("Entering Raffle...");
            const tx = await raffle.enterRaffle({ value: raffleEntranceFee });
            await tx.wait(1);
            console.log("Ok, time to wait...");
            const winnerStartingBalance = await accounts[0].getBalance();

            // and this code WONT complete until our listener has finished listening!
          });
        });
      });
    });

I am facing this error here: 1) Raffle Staging Tests "before each" hook for "works with live Chainlink Keepers and Chainlink VRF, we get a random winner": Error: No Contract deployed with name Raffle at Object.getContract (node_modules/@nomiclabs/hardhat-ethers/src/internal/helpers.ts:447:11) at processTicksAndRejections (node:internal/process/task_queues:96:5) at Context. (test/staging/Raffle.staging.test.js:12:18)

can anybody help me out with this?

1 Answer 1

0

Are you sure this code block is valid?

        deployer = (await getNamedAccounts()).deployer;
        raffle = await ethers.getContract("Raffle", deployer);

I would say it is not correct, but I also assume there are may be multiple ways to deploy a contract in tests.

Here is how I did a similar case:

import { time, loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import "./SwapChainV2";
import "./SwapValue";

describe.only("SwapChain V2 test", async function () {

    async function deploy() {
        const factoryUtils = await ethers.getContractFactory("Utils");
        const contractUtils = await factoryUtils.deploy();

        const factorySwapValue = await ethers.getContractFactory("SwapValue");
        const contractValue = await factorySwapValue.deploy();

        const factory = await ethers.getContractFactory("SwapChainV2");
        const contractSwapChain = await factory.deploy(
            contractUtils.address, 
            contractValue.address
        );

        const accounts = await ethers.getSigners();

        return { contractSwapChain, contractValue, contractUtils }
    }

    it("On register new user, get users call returns relevant count", async function() {
        const { contractSwapChain } = await loadFixture(deploy);
        // the rest of the test case
    }
}

1
  • 1
    hey thanks, this did the job! Jan 10 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.