Skip to main content
edited tags
Link
Sacha Lansky
  • 2.7k
  • 5
  • 31
Source Link
Sacha Lansky
  • 2.7k
  • 5
  • 31

How can I configure genesis state for my pallet's tests without hardcoding it?

I've been writing tests for pallet-kitties and have configured the genesis state inside my mock runtime by setting the storage items for two kitties (one Male and one Female) and their owners (with some balance), that way I can use pre-funded accounts and existing kitties in my tests. So, in mock.rs, I have:

pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
    let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
    GenesisConfig {
        balances: BalancesConfig {
            balances: vec![(1,  10), (2,  10)]
        },
        substrate_kitties: SubstrateKittiesConfig {
            kitties: vec![
                (1, *b"1234567890123456", Gender::Female),
                (2, *b"123456789012345a", Gender::Male)
            ]
        },
        ..Default::default()
    }
        .assimilate_storage(&mut t)
        .unwrap();

    let mut ext = sp_io::TestExternalities::new(t);
    ext.execute_with(|| System::set_block_number(1));
    ext
}

For one of my tests, I need a third, uniquely owned kitty which means I have to add that kitty and a new account in the genesis state of my mock runtime. This doesn't feel like the best approach because I'd be using genesis state for all of my tests — even those that don't need the third kitty.

How can I configure genesis so I can specify genesis state as parameters for my tests instead?