If I have a pallet that depends on the staking pallet, and for example one of the functions needs access to the active era, how can I set up the test environment so that I can actually test this?

Let's say I am accessing the active era like this:

let active_era = Staking::<T>::active_era().ok_or(/*...*/)

I have checked the mock.rs file in the staking pallet directory, and it sets up all the configs and sets up the environment needed for testing the staking pallet.

For my pallet, I need to set up the staking pallet because my pallet is dependent on it. I was thinking of accessing the ExtBuilder from the staking pallet, but it is a bit more complex than just importing it at the beginning of the file. It's because the staking/src/mock.rs file is only compiled when testing the pallet. So when I am testing only my pallet it isn't possible to access the ExtBuilder from the staking pallet. This means that I can't access anything from the mock.rs file inside the staking pallet.

How should I go about setting up the staking pallet for testing my own pallet?

1 Answer 1


You will need to add the Staking Pallet into your mock construct_runtime!:

From frame/session/benchmarking/src/mock.rs:

    pub enum Test where
        Block = Block,
        NodeBlock = Block,
        UncheckedExtrinsic = UncheckedExtrinsic,
        System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
        Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
        Staking: pallet_staking::{Pallet, Call, Config<T>, Storage, Event<T>},
        Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>},

At this point, you should be able to access public functions from the staking module via the Staking struct which is defined in the construct_runtime!.

So you should be able to call:

let active_era = Staking::<T>::active_era().ok_or(/*...*/)

Exactly as you have wanted in your main post.

  • Yeah, I am aware of that, I am interested in setting up the environment for testing. Currently, if I call the active_era function like this I will get None as the result. I need to have some active era and also, define some stakers and potentially nominators like it is done in the staking pallet. I should have been more specific in my question.
    – Szegoo
    Aug 18, 2022 at 12:00
  • 1
    It seems you will need to also add the Session pallet. Take a look at the tests here: github.com/paritytech/substrate/blob/master/frame/staking/src/…
    – Shawn Tabrizi
    Aug 18, 2022 at 12:06
  • 1
    I think I have found a solution actually, I just need to configure the GenesisConfig of the staking pallet if I am not mistaken(for configuring stakers). Edit: For the active era I will look into the tests you have sent me :)
    – Szegoo
    Aug 18, 2022 at 12:06

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.