Contracting testing: get NFT token id

Trying to get the NFT id for writing a simple contract test

Having a similar issue like here:

Where I'm not able to get the token ID for an NFT Contract:

:memo:Details

Using the ERC721PresetMinterPauserAutoIdUpgradeable.sol contract

the CountersUpgradeable.Counter does assign a _tokenIdTracker to the minted NFT

as suggested from @abcoathup tried to get the NFT with balanceOf method.

console.log('balance of', await this.DEPLOYED_CONTRACT_INSTANCE.balanceOf(accounts[0]));

Getting either balance of BN { negative: 0, words: [ 0, <1 empty item> ], length: 1, red: null }
or with .toString() method 0

:computer: Environment
"@openzeppelin/contracts-upgradeable": "^4.0.0",
"truffle": "^5.3.1"

:1234: Code to reproduce

https://github.com/selimerunkut/nft_troubleshoot

1 Like

Hi @intoverq,

Please note, when using an upgradeable contract you need to initialize it.

You donā€™t have an initialize function yet, see:
https://docs.openzeppelin.com/contracts/4.x/upgradeable#usage

Thanks for the information, updated the code.

Found a test implementation from openzepplin and that is passing without the ā€œinitializeā€ function.

it('deployer can mint tokens', async function () {
  const tokenId = new BN('0');

  const receipt = await this.token.mint(other, { from: deployer });
  expectEvent(receipt, 'Transfer', { from: ZERO_ADDRESS, to: other, tokenId });

  expect(await this.token.balanceOf(other)).to.be.bignumber.equal('1');
  expect(await this.token.ownerOf(tokenId)).to.equal(other);

  expect(await this.token.tokenURI(tokenId)).to.equal(baseURI + tokenId);
});
1 Like