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



我在测试合约时得到这个错误。TypeError: Cannot read property of undefined (reading 'address')

const{expect} = require("chai");
const hre = require("hardhat");

describe("Token Contract", function(){
it("Deployment should assign the totalSupply of tokens to the owner", async function()
{
const signers = await ethers.getSigner();
const owner = signers[0];
console.log("Signers Object:", owner);
const Token = await ethers.getContractFactory("Token");
const hardhatToken = await Token.deploy();
const ownerBalance = await hardhatToken.balanceOf(owner.address);
expect(await hardhatToken.totalSupply()).to.be.equal(ownerBalance);
});

});

将这一行添加到hardhatconfig.js中:要求("@nomiclabs/hardhat-waffle";

并将其添加到testfile中:Const{醚}= require("hardhat");…….即使在遵循hardhat官方文档之后,我也面临同样的错误,但添加上述代码对我有效

只需添加——require("@nomicfoundation/hardhat-toolbox">---- on hardhat.config.js

你打错了。

// this is wrong
const signers = await ethers.getSigner();
// this is correct
const signers = await ethers.getSigners();

getSigner仍然是一个有效的命令,但是它不返回一个数组。这就是为什么signers[0]返回undefined