truffle/hardhat 单元测试如何切换不同的账号(地址)
truffle/hardhat 部署完合约之后,调用合约的方法,此时msg.sender是合约的owner
it("转币测试", async function () {
await deployments.fixture(["KanamitTrade"]);
const { tokenOwner, deployer, user0, user1 } = await getNamedAccounts();
const KanamitTrade = await ethers.getContract("KanamitTrade");
//deposit
KanamitTrade.deposit({ value: ethers.utils.parseEther("1.2345678") });
}
直接改 msg.sender会报错
//deposit
KanamitTrade.deposit({ sender:user0, value: ethers.utils.parseEther("1.2345678") });
it("222222222", async function () {
const ftryKTrade = await ethers.getContractFactory("KanamitTrade");
const KanamitTrade = await ftryKTrade.deploy(KanamitCore.address);
await KanamitTrade.deployed();
let signer1 = ethers.provider.getSigner(1);
//deposit
await KanamitTrade.connect(signer1).deposit({ value: ethers.utils.parseEther("1.2345678") });
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!