aelf Tech Talks — AElf Smart Contract Development-The First AElf Smart Contract — Part 2

0 2


AElf Contract TestKit is a testing framework specifically used to test AElf smart contracts. In this framework, it constructs a Stub and uses the methods provided by the Stub instance to simulate transaction executions (generally corresponding to the contract’s Action method). It also queries (usually corresponding to the View method of the contract), in addition to querying the transaction execution results in the test case. Following this, you complete the test task of the contract method.

In the test folder, create an xUnit project as the AELf smart contract test project, or modify the csproj file to:

If the current project needs to use a Stub of a contract to simulate sending or querying transactions, use ContractStub tag to reference the proto file.

  • RootNamespace explicitly specifies a default namespace under this project. The default namespace is changed to be consistent with the contract code. This is not necessary.
  • You can decide whether to add a reference to the third-party class library.
  • A reference to the AELf.Contracts.TestKit of the main chain needs to be added. At the time of writing this document, the latest AELf released version is 0.9.0.
  • Because the purpose of this project is to test the HelloWorld contract, we need to add a reference to the contract project.
  • When the test environment is initialized, the HelloWorld contract needs to be deployed with zero contracts, which means that it is also necessary to use the ContractStub tag to reference the stub of the zero contract.

XXModule is a unit for modular management of the code by the ABP framework. For contract test case projects, it only needs to rely on ContractTestModule because AELf has closed the permission to arbitrarily deploy the contract by default. When preparing the test environment, you need to manually turn on the permission to deploy the contract.

Test Base is used to initialize the variables used in the test case (such as the contract stub and contract address, etc.) and to deploy the contract for the test.

In HelloWorldContractTestBase, we deployed the HelloWorld contract by calling the zero contract DeploySystemSmartContract method, and initialized the two important variables in the contract test case, HelloWorldContractStub and HelloWorldContractAddress.

When the Test Base is fully prepared, the writing part would be easy.

If you want to simulate the process of sending transactions in the test case, for example, you want to send a Greet transaction in the HelloWorld contract, you can directly use the initialized HelloWorldContractStub in Test Base, and call await HelloWorldContractStub.Greet.SendAsync (new Empty ()). After the call is over, a variable of type TransactionResult is used to receive the return value, and the execution result of this transaction is checked.

The following are the most basic test cases for the three methods of Greet, GreetTo, and GetGreetedList:

Please note that the premise of using SendAsync is to assume that the corresponding transaction must be successfully executed when writing the test case. If you want to test the exception of transaction execution failure, you need to use another method: SendWithExceptionAsync.

In Part 3 of this series, we will discuss the deployment of the smart contracts we have developed.

You might also like

Pin It on Pinterest

Share This

Share this post with your friends!

WhatsApp chat