类型错误:无法读取未定义的属性(读取"等于")



我已经创建了2个测试——

在第二次测试中,根据官方硬帽文档,我在[]中附上了所有者addr1, addr2,如const [owner,addr1,addr2] = await ethers.getSigners();,但问题是,当我使用[]括号时,它显示了错误TypeError: Cannot read properties of undefined (reading 'equal'),测试也失败了,

代码——>

const { expect } = require('chai');
// const { ethers } = require('hardhat');

describe('Token contract', function () {
//1st TEST
it('Deployment should assign the total supply of the tokens to the owner', async function () {
const [owner] = await ethers.getSigners();

const Token = await ethers.getContractFactory('Token');
const hardhatToken = await Token.deploy();
const ownerBalance = await hardhatToken.balanceOf(owner.address);

expect(await hardhatToken.totalSupply()).to.equal(ownerBalance);
});
//2nd TEST
it('Should Transfer Tokens between accounts', async function () {

const [owner,addr1,addr2] = await ethers.getSigners();

const Token = await ethers.getContractFactory('Token');
const hardhatToken = await Token.deploy();
//Transfer 10 tokens from Owner to addr1
await hardhatToken.transfer(addr1.address,10);
expect(await hardhatToken.balanceOf(addr1.address).to.equal(10));
//Transfer 5 tokens from addr1 to addr2
await hardhatToken.connect(addr1).transfer(addr2.address,5);
expect(await hardhatToken.balanceOf(addr2.address).to.equal(5))
});
});

但是如果你在第一次测试中看到,我没有使用[],作为所有者,所以测试通过了。下面是官方的Hardhat文档,如果你想检查代码——>

https://hardhat.org/tutorial/testing-contracts.html

请帮我解决这个问题由于

输入图片描述

在第二个测试中,您没有正确地关闭expect调用周围的括号。您正在访问.to.balanceOf返回的数字。

替换为:

expect(await hardhatToken.balanceOf(addr1.address)).to.equal(10);
// ...
expect(await hardhatToken.balanceOf(addr2.address)).to.equal(5);

最新更新