我想编写一个测试用例来检查我的withdraw
函数是否正常工作。代码如下所示:
function withdraw(uint _amount, address _to) public onlyAuthors {
require(!isNullAddress(_to));
require(_amount <= this.balance);
_to.transfer(_amount);
}
但在测试用例中,似乎this.balance
或[some_address].balance
不可用。有没有什么好方法可以在单元测试中测试这种功能?
答案 0 :(得分:0)
1)签订合同工厂:
import web3 from './web3';
import contractFactory from './build/ContractFactory.json';
const instance = new web3.eth.Contract(
JSON.parse(contractFactory.interface),
'<address of deployed contract>'
);
export default instance;
2)在您的测试中,您新建合同并执行必要的操作以使您想要测试的状态达到平衡。在您的测试文件中:
const compiledFactory = require('../ethereum/build/ContractFactory.json');
const compiledContract = require('../ethereum/build/Contract.json');
beforeEach(async () => {
accounts = await web3.eth.getAccounts();
factory = await new web3.eth.Contract(JSON.parse(compiledFactory.interface))
.deploy({ data : compiledFactory.bytecode })
.send({ from : accounts[0], gas : '1000000' });
factory.setProvider(provider);
await factory.methods.createContract('100').send({
from : accounts[ 0 ],
gas : '1000000'
});
等。此步骤将根据您的合同而有所不同。
3)尝试与合同互动(E.G。尝试提取超过您允许的金额或其他任何金额。然后期待您正在测试的行为。
答案 1 :(得分:0)
我认为您可以使用$credentials = array(
'key' => env('AWS_IAM_KEY', ''),
'secret' => env('AWS_IAM_SECRET', '')
);
//2014-06-30
$client = CognitoIdentityClient::factory(array('region' => env('AWS_REGION',''), 'version' => 'latest', $credentials));
$result = $client->adminInitiateAuth([
'AuthFlow' => 'ADMIN_NO_SRP_AUTH',
'ClientId' => COGNITO_APP_CLIENT_ID,
'UserPoolId' => COGNITO_USER_POOL_ID,
'AuthParameters' => [
'USERNAME' => "name",
'PASSWORD' => 'password',
],
]);
$accessToken = $result->get('AuthenticationResult')['AccessToken'];
来检索合约余额。