Skip to content

Getting Started with Unit Tests

Matthew Little edited this page Oct 1, 2018 · 23 revisions

Install Prerequisites

Setup Project

Open command line / terminal and create a new C# project with the command dotnet new console --name YOUR_PROJECT_NAME

Example:

dotnet new console --name MyProject

Add the Meadow unit testing framework package with the command dotnet add package Meadow.UnitTestTemplate

cd MyProject
dotnet add package Meadow.UnitTestTemplate

Create a directory named contracts to place your Solidity source files (the directory must be named contracts).

mkdir contracts

Add your Solidity source files to the contracts directory.

Here's an example hello world Solidity contract that we'll save to our contracts directory as HelloWorld.sol. Solidity source files must have the .sol file extension.

pragma solidity ^0.4.24;

contract HelloWorld {

    event HelloEvent(string _message, address _sender);

    function renderHelloWorld () public returns (string) {
        emit HelloEvent("Hello world", msg.sender);
        return 'Hello world';
    }

}

Run dotnet build and you should see a GeneratedContracts directory with generated source files matching your Solidity contracts. In our example we'll have GeneratedContracts/HelloWorld.sol.cs.

Create a .cs file in your project directory to add our contract test code. For example HelloWorldTests.cs.

Write a test class that 1) deploys our contract, 2) tests a function call result, and 3) tests a transaction execution and its event log.

using Meadow.Contract;
using Meadow.JsonRpc.Types;
using Meadow.UnitTestTemplate;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;

namespace MyProject
{
    [TestClass]
    public class HelloWorldTests : ContractTest
    {
        HelloWorld _contract;

        protected override async Task BeforeEach()
        {
            // Deploy our test contract
            _contract = await HelloWorld.New(RpcClient);
        }

        [TestMethod]
        public async Task ValidateCallResult()
        {
            // Call the renderHelloWorld function and get the return value.
            var callResult = await _contract.renderHelloWorld().Call();

            // Validate the return value is what we expect.
            Assert.AreEqual("Hello world", callResult);
        }

        [TestMethod]
        public async Task ValidateTransactionEventResult()
        {
            // Execute the renderHelloWorld function as a transaction and get the receipt.
            var receipt = await _contract.renderHelloWorld().TransactionReceipt();

            // Get the event log from receipt that we are expecting.
            var eventLog = receipt.FirstEventLog<HelloWorld.HelloEvent>();

            // Validate the event log arg is what we expect.
            Assert.AreEqual("Hello world", eventLog._message);
        }
        
        [TestMethod]
        public async Task ValidateTransactionSender()
        {
            // An array of accounts is available as:
            var fromAccount = Accounts[5];

            // An RPC client instance is available, example:
            var balance = await RpcClient.GetBalance(fromAccount, BlockParameterType.Latest);

            // By default the first RPC account is used for calls and transactions.
            // We can specify which account to use with TransactionParams..
            var txParams = new TransactionParams { From = Accounts[5] };
            var receipt = await _contract.renderHelloWorld().TransactionReceipt(txParams);

            // Validate the msg.sender as echoed back in our example event log matches.
            var eventLog = receipt.FirstEventLog<HelloWorld.HelloEvent>();
            Assert.AreEqual(eventLog._sender, fromAccount);
        }
    }
}

Temp - command to add latest beta package dotnet add package Meadow.UnitTestTemplate -v 0.3.185-beta -s https://www.myget.org/F/hosho/api/v3/index.json

Clone this wiki locally