0

I would like to avoid the most pallet config initialisation and just want to use the actual runtime configuration for the (unit) tests. This would simplify my mock process a lot. The only thing that comes into mind is to mock the balances.

So I need to implement ExtBuilder. The reason to take this approach is because I always need to copy stuff from my actual runtime to the mock, but in my case it's always the same configuration.

1 Answer 1

1

It is important to have the mock.rs inside the runtime folder. Then you can just import all runtime parameters with use crate::*; inside the mock.rs.

Just found out that I can just use Runtime instead of Test as a generic to all instances of the test and mock. In addition to that, you need to create Accounts as actual accounts and not just numbers.

use sp_core::{sr25519, Pair, Public, H256};
use sp_runtime::traits::{IdentifyAccount, Verify};

type AccountPublic = <Signature as Verify>::Signer;

/// Helper function to generate a crypto pair from seed
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
    TPublic::Pair::from_string(&format!("//{seed}"), None)
        .expect("static values are valid; qed")
        .public()
}

/// Helper function to generate an account ID from seed
pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId
where
    AccountPublic: From<<TPublic::Pair as Pair>::Public>,
{
    AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
}

pub fn alice() -> AccountId {
    get_account_id_from_seed::<sr25519::Public>("Alice")
}

pub fn bob() -> AccountId {
    get_account_id_from_seed::<sr25519::Public>("Bob")
}

pub fn charlie() -> AccountId {
    get_account_id_from_seed::<sr25519::Public>("Charlie")
}

pub fn dave() -> AccountId {
    get_account_id_from_seed::<sr25519::Public>("Dave")
}

pub fn eva() -> AccountId {
    get_account_id_from_seed::<sr25519::Public>("Eva")
}

pub fn fred() -> AccountId {
    get_account_id_from_seed::<sr25519::Public>("Fred")
}

You can leave the ExtBuilder as it is. It just builds the execution environment with the actual runtime as a charm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.