Acton
Testing

Cookbook

Practical examples and code snippets for common testing scenarios in Tolk smart contracts

Basic assertions

get fun `test some`() {
    expect(1 + 2).toEqual(3);
}

Sending internal message from one contract to another

get fun `test mint`() {
    // ...
    val msg = createMessage({ // same createMessage as in contracts
        bounce: false,
        value: ton("0.05"),
        dest: address,
        body: ChangeMinterAdmin {
            queryId: 0,
            newAdminAddress: newOwner,
        },
    });
    val result = net.send(from, msg);
    println(result); // show transaction tree
}

See Reading Transaction Chains for the exact text format and a guide to interpreting it.

Calling get method

fun JettonMinter.getJettonData(self): JettonDataReply {
    return net.runGetMethod(self.address, "get_jetton_data");
}

Calling get method with an argument

fun JettonMinter.getWalletAddress(self, owner: address): address {
    return net.runGetMethod(self.address, "get_wallet_address", [owner]);
}

Creating a treasury

import "@acton/emulation/testing"

get fun `test some`() {
    val treasury = testing.treasury("treasury");
    // ..
    net.send(treasury.address, msg);
}

Asserting successful deploy

get fun `test mint`() {
    // ...
    expect(transactions).toHaveSuccessfulDeploy({ to: minter.address });
}

Asserting successful transaction in the result

get fun `test mint`() {
    // ...
    expect(mintResult).toHaveSuccessfulTx({
        from: minter.address,
        to: wallet.address,
    });
}

Asserting successful transaction with specific message in the result

get fun `test mint`() {
    // ...
    expect(mintResult).toHaveSuccessfulTx<MintNewJettons>({
        from: minter.address,
        to: wallet.address,
    });
}

You can also specify the message type in some of the methods described below.

Asserting transaction in the result

get fun `test mint`() {
    // ...
    expect(transactions).toHaveTx({
        from: deployer.address,
        to: counter.address,
    });
}

Asserting bounced transaction in the result

get fun `test mint`() {
    // ...
    expect(sendResult).toHaveBouncedTx({
        from: deployerJettonWallet.address,
        to: deployer.address,
    });
}

Asserting fail transaction in the result

get fun `test mint`() {
    // ...
    expect(mintResult).toHaveFailedTx({
        from: notDeployer.address,
        to: minter.address,
        exitCode: ERR_NOT_FROM_ADMIN,
    });
}

Asserting transaction absence in the result

get fun `test mint`() {
    // ...
    expect(sendResult).toNotHaveTx({
        from: notDeployerJettonWallet.address,
        to: notDeployer.address
    });
}

Asserting external message transaction in the result

get fun `test something`() {
    // ...
    expect(result).toEmitExternalMessage<SellEvent>();
}

Asserting gas consumption

get fun `test something`() {
    // ...
    expect(sendResult.at(0)).toConsumeLessThan(1500);
}

Getting out actions of the result

get fun `test something`() {
    // ...
    val outActions = sendResult.at(0).allOutActions();
    val first = outActions.at(0);
    println(first.kind());
}

Creating a random address

import "@acton/emulation/network"

get fun `test something`() {
    val addr = randomAddress("name-to-display");
}

Check if contract is deployed

import "@acton/emulation/testing"

fun JettonWallet.getJettonBalance(self): coins {
    if (!testing.isDeployed(self.address)) {
        return 0;
    }

    val result = net.runGetMethod<JettonWalletDataReply>(self.address, "get_wallet_data");
    return result.jettonBalance;
}

Inspect deployed account state

import "@acton/emulation/testing"

get fun `test something`() {
    val state = testing.getAccountState(addr);
    if (state != null) {
        println("Balance: {:ton}", state.storage.balance.grams);
    }
}

Register a library

import "@acton/emulation/testing"

get fun `test something`() {
    testing.registerLibrary(lib_cell);
    // ...
}

Last updated on

On this page