This part of own Q&A serial of posts which shares knowledge gained during work on one project
I was need to test my contract under different accounts and was looking for ways to do this.
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityIn Hardhat case , u first need to get your hardhat blockchain simulated accounts using ethers.getSigners() function. Please know it returns an array with Signer Objects. When you have the accounts u can use the contract.connect method to connect your contract with the desired Signer Account u got from getSigners() function.
https://hardhat.org/hardhat-runner/plugins/nomiclabs-hardhat-ethers https://docs.ethers.io/v5/api/contract/contract/
let [account1,account2,account3] = await ethers.getSigners();
await contract.connect(account2).method();
ether.js
allows to do this in this way:
contract.connect(anotherUser).transfer();
Also I saw an option to create a separate contract which would act as Actor to verify test cases.