substratepolkadot

How to test events in substrate 3.0.0?


In substrate 2.0.1, its has following code:

mod simple_event {
    pub use crate::Event;
}

impl_outer_event! {
    pub enum TestEvent for TestRuntime {
        simple_event,
        frame_system<T>,
    }
}

https://github.com/substrate-developer-hub/recipes/blob/master/pallets/simple-event/src/tests.rs

But adding impl_outer_event is giving error in substrate 3.0.0:

 / frame_support::construct_runtime!(
14 | |     pub enum Test where
15 | |         Block = Block,
16 | |         NodeBlock = Block,
...  |
21 | |     }
22 | | );
   | |__^ duplicate definitions for `outer_event_metadata`
...
58 | / impl_outer_event! {
59 | |     pub enum TestEvent for Test {
60 | |         simple_event<T>,
61 | |         frame_system<T>,
62 | |     }
63 | | }
   | |_- other definition for `outer_event_metadata`

https://github.com/substrate-developer-hub/substrate-node-template/blob/master/pallets/template/src/mock.rs

How to solve it?


Solution

  • In Substrate 3.0 you use the same construct_runtime! macro in tests that you use in the full runtime. You can see an example of how to test events the new way in the Substrate repository itself.

    https://github.com/paritytech/substrate/blob/83942f58fc859ef5790351691e1ef665d79f0ead/frame/balances/src/tests.rs#L470-L473