I got an error while running truffle test on a smart contract, can anyone help me with this?
const KryptoBird = artifacts.require("KryptoBird");
// check for chai
require('chai')
.use(require('chai-as-promised'))
.should()
contract('KryptoBird', (accounts) => {
let contract
before( async () => {
contract = await KryptoBird.deployed()
})
describe('deployment', async() => {
it("deploys successfully", async () => {
const address = contract.address;
assert.notEqual(address, '')
assert.notEqual(address, null)
assert.notEqual(address, undefined)
assert.notEqual(address, 0x0)
})
it('has a name', async() => {
const name = await contract.name()
assert.equal(name, 'KryptoBird')
})
it('has a symbol', async() => {
const symbol = await contract.symbol()
assert.equal(symbol, 'KBIRDZ')
})
})
describe('minting', async ()=> {
it('creates a new token', async ()=> {
const result = await contract.mint('https...1')
const totalSupply = await contract.totalSupply()
assert.equal(totalSupply, 1)
const event = result.logs[0].args
assert.equal(event._from, '0x0000000000000000000000000000000000000000', 'from is the contract')
assert.equal(event._to, accounts[0], 'to is msg.sender')
await contract.mint('https...1').should.be.rejected
})
})
})
this is my code, and when I run it, it gives me an error that I don't understand this is the error:
1 ) Contract : KryptoBird
minting
creates a new token :
AssertionError : expected promise to be rejected but it was fulfilled with { Object ( tx , receipt , ... ) }
please help me, I've been stuck on this for a day.
heres my smart contract code, if u could help me thanks a lot! :
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import './ERC721Connecter.sol';
contract KryptoBird is ERC721Connecter {
string[] public kryptoBirdz;
mapping(string => bool) _kryptoBirdzExists;
function mint(string memory _kryptoBird) public {
require(!_kryptoBirdzExists[_kryptoBird],
'Error - kryptoBird already exists');
// this is deprecated - uint _id = KryptoBirdz.push(_kryptoBird);
kryptoBirdz.push(_kryptoBird);
uint _id = kryptoBirdz.length - 1;
// .push no logner returns the length but a ref to the added element
_mint(msg.sender, _id);
}
constructor() ERC721Connecter('KryptoBird','KBIRDZ')
{}
}
CodePudding user response:
The problem is not you are not adding entry into the _kryptoBirdzExists
mapping. before you mint, you are checking this condition
require(!_kryptoBirdzExists[_kryptoBird],
So after minting, you should be updating the mapping
_kryptoBirdzExists[_kryptoBird]=true;
Your test is not failing because you never added the 'https...1' to the mapping so require
stament was passing so you were minting again