1

在基板 2.0.1 中,它具有以下代码:

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

但是添加 impl_outer_event 会在基板 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

如何解决?

4

2 回答 2

3

在 Substrate 3.0construct_runtime!中,您在测试中使用与在完整运行时中使用的相同的宏。您可以在 Substrate 存储库本身中看到如何以新方式测试事件的示例。

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

于 2021-03-22T18:33:39.373 回答
2

这就是我所做的。

mock.rs文件中,我改为:

pub fn new_test_ext() -> sp_io::TestExternalities {
    let t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
    let mut ext = sp_io::TestExternalities::new(t);
    ext.execute_with(|| System::set_block_number(1));
    ext
}

接收事件。

在tests.rs中:

use crate::{RawEvent};

#[test]
fn my_test() {

    new_test_ext().execute_with(|| { 
        assert_ok!(TemplateModule::check_peers_deparment(Origin::signed(2), 1));
        let expected_event = Event::pallet_template(RawEvent::PeerDepartment(1, 2));
        assert_eq!(System::events()[0].event, expected_event);
    });

}
于 2021-03-23T07:00:42.503 回答