0

I am trying to write a unit test for the simple condition of checking the owner's address once the contract is deployed.

The unit test code for this is as follows:

const { ethers } = require("hardhat");

describe("SmartWallet Testing", function()
{
    it("Check Owner", async function()
    {
     const Owneradd = await ethers.getSigners();
     const SmartWallet = await ethers.getContractFactory("SmartWallet");
     const deploy = await SmartWallet.deploy();
     
     expect(await deploy.owner()).to.equal(Owneradd.address);
    });
});```

The error I encounter is **AssertionError: expected '0xCAEc8498aB11D596116B5175017B9Ab220b…' to equal undefined**

I am using the Signer function to get a default address, but not sure why it doesn't work.

2 Answers 2

1

This is the incorrect way of getting the owner's address using getSigners()
As getSigners() function will return multiple signers so first you need to get the owner from getSigners().

//destructuring owner from getSigners  
const [owner] = await ethers.getSigners();

Now you can use expect to test

expect(await deploy.owner()).to.equal(owner.address);

Your entire it block should look like


    it('Check Owner', async function () {
            const [owner] = await ethers.getSigners();
            const SmartWallet = await ethers.getContractFactory('SmartWallet');
            const deploy = await SmartWallet.deploy();
            expect(await deploy.owner()).to.equal(owner.address);
        });

1
  • Thanks, Neeraj that was really helpful, I figured this out last night and came to the same conclusion. Apr 26, 2023 at 1:53
1

Have you tried with:

  const deployeAddress = await deploy.owner()
  expect(deployerAddress).to.equal(Owneradd.address);

Hope it helps!

1
  • Thank you. I have another question, If I have deployed a contract using the deploy() method, and I want to get the contract address of the deployed contract what can I do? Apr 26, 2023 at 2:29

Your Answer

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

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