简体   繁体   English

安全帽测试:<functionname> 不是 function</functionname>

[英]hardhat test: <functionName> is not a function

i'm testing my contracts on hardhat network with fork of BSC.我正在使用 BSC 的分支在安全帽网络上测试我的合约。

i'm deploying my token contract that have mint function:我正在部署具有薄荷 function 的代币合约:

// @dev Creates `_amount` token to `_to`. Must only be called by the owner (MasterChef).
function mint(address _to, uint256 _amount) public onlyOwner {
    _mint(_to, _amount);
    _moveDelegates(address(0), _delegates[_to], _amount);
}

then i'm deploying it on test using > npx hardhat test , it will run tests of this code:然后我使用> npx hardhat test在测试中部署它,它将运行此代码的测试:

...
it("Should deploy", async () => {
        token = await Token.deploy();
        await token.deployed();
        console.debug(`\t\t\tToken Contract Address: ${cyan}`, token.address);
        const supply = await token.totalSupply()
        console.debug(`\t\t\tToken totalSupply: ${yellow}`, supply);
        await token.mint(owner.address, web3.utils.toWei("1000", 'ether'))
        console.debug(`\t\t\tToken owner balance: ${cyan}`, token.balanceOf(owner.address));
 });
 ...

test print the first 2 console debug correctly :测试正确打印前 2 个控制台调试:

 Token Contract Address: 0x5FbDB2315678afecb367f032d93F642f64180aa3
 Token totalSupply: 0

also token.totalSupply() works, so the token is deployed correctly, but when it have to call token.mint() it give this error: token.totalSupply()也有效,因此令牌已正确部署,但是当它必须调用token.mint()时,它会出现此错误:

 TypeError: token.mint is not a function
  at Context.<anonymous> (test/general.js:102:21)
  at runMicrotasks (<anonymous>)
  at processTicksAndRejections (internal/process/task_queues.js:95:5)

i tried to clean all the artifacts running > npx hardhat clean and delated all the cache, but i still have the error我试图清理所有正在运行的工件> npx hardhat clean并删除所有缓存,但我仍然有错误

if you declared two mint functions then you have to explicitly use the fully qualified signature.如果您声明了两个 mint 函数,那么您必须显式使用完全限定签名。 Example:例子:

token["mint(address,uint256)"](owner.address, web3.utils.toWei("1000", 'ether'))

I found the solution here https://github.com/ethers-io/ethers.js/issues/407我在这里找到了解决方案https://github.com/ethers-io/ethers.js/issues/407

functionName is not a function this error occurs if the function is not available in your smart contract. functionName is not a function如果 function 在您的智能合约中不可用,则会发生此错误。 in your case the function name is not mint() it's _mint().在您的情况下,function 名称不是 mint() 它是 _mint()。

in beforeEach() block of code get your contract object by -beforeEach()代码块中,通过 -

const token = await ethers.getContractFactory("Token")

If you haven't defined beforeEach() block define it before it() block如果您尚未定义beforeEach()块,请在it()块之前定义它

  beforeEach(async function () {
    Token = await ethers.getContractFactory("Token")
    token = await token.deploy()
  })

define Token and token using let keyword before, also somewhere in your code you may be missing await somewhere so that could be also the reason.之前使用let关键字定义Tokentoken ,在代码中的某个地方你可能会在某个地方缺少await ,这也可能是原因。 Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM