# Contracting testing: get NFT token id

**URL:** <https://forum.openzeppelin.com/t/contracting-testing-get-nft-token-id/7058>\
**Category:** Contracts\
**Tags:** openzeppelin-contracts\
**Created:** [April 16, 2021, 12:50pm UTC](https://forum.openzeppelin.com/t/contracting-testing-get-nft-token-id/7058 "2021-04-16T12:50:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![intoverq](https://avatars.discourse-cdn.com/v4/letter/i/4af34b/32.png) [@intoverq](https://forum.openzeppelin.com/u/intoverq)\
**Post date:** [April 16, 2021, 12:50pm UTC](https://forum.openzeppelin.com/t/contracting-testing-get-nft-token-id/7058/1 "2021-04-16T12:50:35Z")

</div>

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

Having a similar issue like here:

> [@How to check if an address owns an NFT (ERC721)?](https://forum.openzeppelin.com/t/how-to-check-if-an-address-owns-an-nft-erc721/2678):
>
> So I have an ERC721 Contract when I mint NFTs then let users clone them but then they get added to the list of tokens, now I need to know if the current user/address owns this NFT? thanks

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

**📝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

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

**🔢 Code to reproduce**

[https://github.com/selimerunkut/nft\_troubleshoot](https://github.com/selimerunkut/nft_troubleshoot)

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [April 19, 2021, 7:43am UTC](https://forum.openzeppelin.com/t/contracting-testing-get-nft-token-id/7058/2 "2021-04-19T07:43:26Z")

</div>

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](https://docs.openzeppelin.com/contracts/4.x/upgradeable#usage)

---

<div class="post-metadata">

**Author:** ![intoverq](https://avatars.discourse-cdn.com/v4/letter/i/4af34b/32.png) [@intoverq](https://forum.openzeppelin.com/u/intoverq)\
**Post date:** [April 19, 2021, 9:31am UTC](https://forum.openzeppelin.com/t/contracting-testing-get-nft-token-id/7058/3 "2021-04-19T09:31:11Z")

</div>

Thanks for the information, updated the code.

Found a test implementation from openzepplin and that is passing without the “initialize” function.

> <https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/1abdc9e2243c25cd98b5bd6df789a5c686400b7b/test/token/ERC721/presets/ERC721PresetMinterPauserAutoId.test.js>

```
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);
});
```
