ManicMinter e2e Test
In this chapter we will write e2e tests for the ManicMinter contract. The e2e tests will be written in Rust using the ink! e2e framework. The e2e tests will be executed on a local substrate node. Just like in previous chapter we will not include complete code from the contract to keep it short and focused on the e2e tests.
Import Crates
Let's create a new module e2e_tests
within the body of the mod manicminter
and import the following crates:
#[cfg(all(test, feature = "e2e-tests"))]
mod e2e_tests {
use super::*;
use crate::manicminter::ManicMinterRef;
use ink::primitives::AccountId;
use ink_e2e::build_message;
use openbrush::contracts::ownable::ownable_external::Ownable;
use openbrush::contracts::psp22::psp22_external::PSP22;
use oxygen::oxygen::OxygenRef;
type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
const AMOUNT: Balance = 100;
const PRICE: Balance = 10;
}
You will notice that we import Openbrush traits to invoke methods from the Oxygen contract, which is implemented using Openbrush's version of PSP22.
Instantiate Contracts
We will use the ink_e2e::Client
to instantiate the contracts. The ink_e2e::Client
is a wrapper around the ink_env::test
environment. The ink_e2e::Client
provides a convenient way to instantiate contracts and invoke contract methods.
In the declarative macro add our contracts as additional contracts
:
#[ink_e2e::test(additional_contracts = "manicminter/Cargo.toml oxygen/Cargo.toml")]
async fn e2e_minting_works(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
let initial_balance: Balance = 1_000_000;
// Instantiate Oxygen contract
let token_constructor = OxygenRef::new(initial_balance);
let oxygen_account_id = client
.instantiate("oxygen", &ink_e2e::alice(), token_constructor, 0, None)
.await
.expect("token instantiate failed")
.account_id;
// Instantiate ManicMinter contract
let manic_minter_constructor = ManicMinterRef::new(oxygen_account_id);
let manic_minter_account_id = client
.instantiate(
"manic-minter",
&ink_e2e::alice(),
manic_minter_constructor,
0,
None,
)
.await
.expect("ManicMinter instantiate failed")
.account_id;
}