如何解决如何测试基板 3.0.0 中的事件?
在substrate 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`
如何解决?
解决方法
在 Substrate 3.0 中,您在测试中使用与完整运行时相同的 construct_runtime!
宏。您可以在 Substrate 存储库中查看如何以新方式测试事件的示例。
我就是这样做的。
在 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);
});
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。