Hardhat test for ERC721 Upgradeable

I am having a really hard time writing hardhat test for ERC721 Upgradeable contract.
Here's my test code.

describe('BoxNFT', function () {
  it('check the owner', async function () {
    const contractFactory = await ethers.getContractFactory('BoxNFT')
    const [owner] = await ethers.getSigners()
    const contract = await contractFactory.deploy()

    expect(await contract.owner()).to.equal(owner.address)
  }
}

And here's my contract.

contract BoxNFT is ERC721Upgradeable, OwnableUpgradeable, ReentrancyGuardUpgradeable {
    function initialize() public initializer {
        OwnableUpgradeable.__Ownable_init();
        ReentrancyGuardUpgradeable.__ReentrancyGuard_init();
        ERC721Upgradeable.__ERC721_init("Box NFT", "BoxNFT");
    }
}

When I run the test code, I have this error.

 AssertionError: expected '0x0000000000000000000000000000000000000000' to equal '0xf39Fd6e51aad88F6F4ce6aB88272*************'
      + expected - actual

      -0x0000000000000000000000000000000000000000
      +0xf39Fd6e51aad88F6F4ce6aB88272*************

What's wrong there?
Why the owner is 0x0000000000000000000000000000000000000000 instead of 0xf39Fd6e51aad88F6F4ce6aB88272*************?

1 Like

Hi, welcome! :wave:

I think you should try to call initialize() manually.

And there is a tutorial, you can have a look:

Thank you for your reply @Skyge
I was able to fix the issue.
On my code above, initializer function is not calling at all.
So I tried as follows.

 const contractFactory = await ethers.getContractFactory('GoldenDogeNFT')
 const [owner] = await ethers.getSigners()
 const contract = await upgrades.deployProxy(contractFactory, { initializer: 'initialize' })

Then there was another issue.

Cannot find name 'upgrades'

I was missing something for the configuration.
I had to import @openzeppelin/hardhat-upgrades on hardhat.config.js and also import upgrades from harthat on test file.

hardhat.config.ts
import '@openzeppelin/hardhat-upgrades'

test/Box.ts
import { ethers, upgrades } from 'hardhat'

Now it's working fine.
Thank you!

1 Like