在 rust 测试中从 substrate 事件中获取 payload

getting payload from a substrate event back in rust tests

我已经成功创建了我的第一个基板项目,并且构建的托盘也可以正常工作。现在我想为流程和提供的功能创建测试。

我的流程是生成随机散列并将该散列存储到交易的发送方

let _sender = ensure_signed(origin)?;
let nonce = Nonce::get();
let _random_seed = <randomness_collective_flip::Module<T>>::random_seed();
let random_hash = (_random_seed, &_sender, nonce).using_encoded(T::Hashing::hash);


ensure!(!<Hashes<T>>::contains_key(random_hash), "This new id already exists");

let _now = <timestamp::Module<T>>::get();

let new_elem = HashElement {
    id: random_hash,
    parent: parent,
    updated: _now,
    created: _now
};

<Hashes<T>>::insert(random_hash, new_pid);
<HashOwner<T>>::insert(random_hash, &_sender);

Self::deposit_event(RawEvent::Created(random_hash, _sender));

Ok(())

到目前为止效果很好,现在我想通过书面测试来测试流程,我想检查 Created 事件中发出的散列是否也在 HashOwner Map 中分配。为此,我需要从事件中获取价值。

这是我的问题 :D 我在 Rust 方面并不专业,我发现的所有示例都期待在这样的事件中发出的所有值:

// construct event that should be emitted in the method call directly above
let expected_event = TestEvent::generic_event(RawEvent::EmitInput(1, 32));

// iterate through array of `EventRecord`s
assert!(System::events().iter().any(|a| a.event == expected_event));

在调试我的笔试时:

assert_ok!(TemplateModule::create_hash(Origin::signed(1), None));
let events = System::events();

let lastEvent = events.last().unwrap();
let newHash = &lastEvent.event;

我在 VSCode 中看到这些值可用:

debug window of vs code

但我不知道如何在变量中获取此哈希...也许这只是一个衬里...但我对 Rust 的了解太少了 :D

感谢您的帮助

如果您只关心模块放入系统的最后一个事件而不关心其他事件,这里有一个关于如何解析和检查事件的通用示例。

assert_eq!(
    System::events()
        // this gives you an EventRecord { event: ..., ...}
        .into_iter() 
        // map into the inner `event`.
        .map(|r| r.event)
        // the inner event is like `OuterEvent::mdouleEvent(EventEnum)`. The name of the outer 
        // event comes from whatever you have placed in your `delc_event! {}` in test mocks.
        .filter_map(|e| { 
            if let MetaEvent::templateModule(inner) = e {
                Some(inner)
            } else {
                None
            }
        })
        .last()
        .unwrap(),

    // RawEvent is defined and imported in the template.rs file. 
    // val1 and val2 are things that you want to assert against.
    RawEvent::Created(val1, val2),
);

其实你也可以省略第一张图或者用更紧凑的方式来做,但我是这样做的,所以你可以一步步看。

打印System::events(),这也有帮助。

我现在从kianenigma的回复中得到了它:)

我想在事件中重复使用给定的数据:

    let lastEvent = System::events()
    // this gives you an EventRecord { event: ..., ...}
    .into_iter() 
    // map into the inner `event`.
    .map(|r| r.event)
    // the inner event is like `OuterEvent::mdouleEvent(EventEnum)`. The name of the outer 
    // event comes from whatever you have placed in your `delc_event! {}` in test mocks.
    .filter_map(|e| { 
        if let TestEvent::pid(inner) = e {
            Some(inner)
        } else {
            None
        }
    })
    .last()
    .unwrap();
    if let RawEvent::Created(newHash, initiatedAccount) = lastEvent {
        // there are the values :D
    }

这也许可以写得更好,但这对我有帮助 :)