构建实用的去中心化应用
1. 投票合约测试
在投票合约测试中,我们需要确保同一账户不能多次投票。以下代码展示了如何使用 pytest.raises 来捕获交易失败的异常,以此保证同一账户不能重复投票:
with pytest.raises(eth_tester.exceptions.TransactionFailed):
voting.functions.vote(1).transact({'from': account2})
with pytest.raises(eth_tester.exceptions.TransactionFailed):
voting.functions.vote(0).transact({'from': account2})
另外,我们还需要测试获胜提案,以下是测试获胜提案的代码:
def test_winning_proposal(voting, chain):
t = eth_tester.EthereumTester()
account2 = t.get_accounts()[1]
account3 = t.get_accounts()[2]
account4 = t.get_accounts()[3]
set_txn_hash = voting.functions.vote(0).transact({'from': account2})
chain.wait.for_receipt(set_t
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



