I have an Event (ThingStored
) that is deposited in a public function in my custom pallet.
#[derive(Encode, Decode, PartialEq, Clone, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
#[codec(mel_bound())]
pub struct Thing<T: Config> {
pub id: u64,
// pub creator: AccountOf<T>
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
ThingStored(Thing<T>, T::AccountId),
}
Thing
contains data that is generated by the pallet (id
). In my tests, I call the pallet function that stores a thing and emits the event if successful. How can I read the data of the event that was emited?
In this case, I will read the generated id
from the event in order to make a subsequent call to the pallet's storage (by id).
#[test]
fn it_works_for_default_value() {
new_test_ext().execute_with(|| {
// Dispatch a signed extrinsic.
assert_ok!(ThingsModule::create_thing(Origin::signed(1)));
let emitted_stored_thing_event: Event<Test> = <frame_system::Pallet<Test>>::events().pop().expect("Expected at least one EventRecord to be found").event;
// let stored_thing: Thing;
// Read pallet storage and assert an expected result.
assert_eq!(ThingModule::things(thing.id).unwrap().creator, 1);
});
}
hooks
substrate.stackexchange.com/a/972/2019