Unit Testing Transaction
Parse and test transactions with various options
The TxParser
is a tool where you can parse the typical transaction CBOR hex back into the MeshTxBuilderBody
. With such capability, you can proceed with rebuilding a transaction or examing the with unit testing frameworks.
In this page, we will cover how to initialize the TxParser
.
Initialize Tx Parser
To start parsing transaction, you need to first initialize TxParser
:
There are 2 fields to pass in to initialized TxParser
:
serializer
: The serializer instance that will be used for parsing transactionfetcher
(optional):TxParser
requires all inputUTxO
information provided since the transaction CBOR hex only preserves transaction hash and output index. When you are not providing all inputUTxO
information, thefetcher
instance is used to fetch the missingUTxO
Interpret Result
After performing the tests, you can interpret the results of the tests using the success
and errors
methods.
success
: Return a boolean indicating if all tests are passederrors
: Show all the errors that occurred during the tests . If there are no errors, it will return an empty string.
Testing Inputs
Testing inputs starts with locating the inputs you want to test. The filtering will not reset until the filtering methods are called again.
There are multiple methods available to filter the inputs:
allInputs
: not apply filtersinputsAt
: filtering inputs with addressinputsWith
: filtering inputs with tokeninputsWithPolicy
: filtering inputs with policy idinputsAtWith
: filtering inputs with address and tokeninputsAtWithPolicy
: filtering inputs with address and policy id
After applying filters, you can proceed with checking value:
inputsValue
: Check the total value of the filtered inputs
Testing Outputs
Testing outputs starts with locating the outputs you want to test. The filtering will not reset until the filtering methods are called again.
There are multiple methods available to filter the outputs:
allOutputs
: not apply filtersoutputsAt
: filtering outputs with addressoutputsWith
: filtering outputs with tokenoutputsWithPolicy
: filtering outputs with policy idoutputsAtWith
: filtering outputs with address and tokenoutputsAtWithPolicy
: filtering outputs with address and policy id
After applying filters, you can proceed with checking value:
outputsValue
: Check the total value of the filtered outputsoutputsInlineDatumExist
: Check whether any one of the outputs contains inline datum (provided as CBOR)
Testing Mints
Testing mints with below APIs:
tokenMinted
: Checks if a specific token is minted in the transaction.onlyTokenMinted
: Checks if a specific token is minted in the transaction and that it is the only mint.policyOnlyMintedToken
: Checks if a specific token is minted in the transaction, ensuring that it is the only mint for the given policy ID.checkPolicyOnlyBurn
: Checks if a specific policy ID is burned in the transaction, ensuring that it is the only minting (i.e. burning item).
Testing Time
Testing time with below APIs:
validAfter
: Checks if the transaction is valid after a specified timestamp.validBefore
: Checks if the transaction is valid before a specified timestamp.
Testing Signature
Testing time with below APIs:
keySigned
: Checks if a specific key is signed in the transaction.oneOfKeysSigned
: Checks if any one of the specified keys is signed in the transaction.allKeysSigned
: Checks if all specified keys are signed in the transaction.