任务说明:
这个任务是基于分支refactor/vote-contract-tests,所以有兴趣的人投票合同的重构测试用例需要从签他支dev到refactor/vote-contract-tests。(或者根据此分支在您自己的仓库上创建一个新分支。)
检查项目test/AElf.Contracts.AEDPoSExtension.Demo.Tests基本知道如何使用TestKit AEDPoS扩展。 测试用例DemoTest(如下图所示)显示了区块链系统的工作原理:选择一些事务然后将它们打包到一个块,每个新块都基于前一个块。 通过使用XXStub,您可以像调用/发送事务的特定用户一样调用测试用例中的特定合同方法。此外,您可以XXStub用于生成交易。
[Fact] public async Task DemoTest() { // Check round information after initialization. { var round = await ConsensusStub.GetCurrentRoundInformation.CallAsync(new Empty()); round.RoundNumber.ShouldBe(1); round.TermNumber.ShouldBe(1); round.RealTimeMinersInformation.Count.ShouldBe(AEDPoSExtensionConstants.InitialKeyPairCount); } // We can use this method process testing. // Basically this will produce one block with no transaction. await BlockMiningService.MineBlockAsync(); // And this will produce one block with one transaction. // This transaction will call Create method of Token Contract. await BlockMiningService.MineBlockAsync(new List<Transaction> { TokenStub.Create.GetTransaction(new CreateInput { Symbol = "ELF", Decimals = 8, TokenName = "Test", Issuer = Address.FromPublicKey(SampleECKeyPairs.KeyPairs[0].PublicKey), IsBurnable = true, TotalSupply = 1_000_000_000_00000000 }) }); // Check whether previous Create transaction successfully executed. { var tokenInfo = await TokenStub.GetTokenInfo.CallAsync(new GetTokenInfoInput {Symbol = "ELF"}); tokenInfo.Symbol.ShouldBe("ELF"); } // Next steps will check whether the AEDPoS process is correct. // Now 2 miners produced block during first round, so there should be 2 miners' OutValue isn't null. { var round = await ConsensusStub.GetCurrentRoundInformation.CallAsync(new Empty()); round.RealTimeMinersInformation.Values.Count(m => m.OutValue != null).ShouldBe(2); } await BlockMiningService.MineBlockAsync(new List<Transaction>()); { var round = await ConsensusStub.GetCurrentRoundInformation.CallAsync(new Empty()); round.RealTimeMinersInformation.Values.Count(m => m.OutValue != null).ShouldBe(3); } // Currently we have 5 miners, and before this line, 3 miners already produced blocks. // 3 more blocks will end current round. for (var i = 0; i < 3; i++) { await BlockMiningService.MineBlockAsync(new List<Transaction>()); } // Check round number. { var round = await ConsensusStub.GetCurrentRoundInformation.CallAsync(new Empty()); round.RoundNumber.ShouldBe(2); } // 6 more blocks will end second round. for (var i = 0; i < 6; i++) { await BlockMiningService.MineBlockAsync(new List<Transaction>()); } // Check round number. { var round = await ConsensusStub.GetCurrentRoundInformation.CallAsync(new Empty()); round.RoundNumber.ShouldBe(3); } }
知道这一点后,您可以去
test/AElf.Contracts.Vote.AEDPoSExtension.Tests
尝试重构投票合同的测试用例。对于测试逻辑,只需复制当前测试用例的逻辑即可
AElf.Contracts.Vote.Tests
。
此外,欢迎重构其他系统合同(位于其中contract/
)。README
in test/AElf.Contracts.AEDPoSExtension.Demo.Tests
解释了如何创建测试项目。