0

我正在尝试ManagedBuffer在 rust 测试中创建一个新的,但测试很恐慌。我试过同时使用ManagedBuffer::new_from_bytes函数和managed_buffer!宏,但它们都导致相同的错误:

线程“use_managed_buffer_new_from_bytes”在“调用Option::unwrap()一个None值”时惊慌失措,/home/mccuna/elrondsdk/vendor-rust/registry/src/github.com-1ecc6299db9ec823/elrond-wasm-debug-0.27.4/src/tx_mock/tx_context_stack。 rs:16:28

线程“use_managed_buffer_macro”在“调用Option::unwrap()一个None值”时惊慌失措,/home/mccuna/elrondsdk/vendor-rust/registry/src/github.com-1ecc6299db9ec823/elrond-wasm-debug-0.27.4/src/tx_mock/tx_context_stack。 rs:16:28

// test_test.rs
use elrond_wasm::types::ManagedBuffer;
use elrond_wasm_debug::{managed_buffer, tx_mock::TxContextRef};

#[test]
fn use_managed_buffer_macro() {
  let test: ManagedBuffer<TxContextRef> = managed_buffer!(b"Test");
}

#[test]
fn use_managed_buffer_new_from_bytes() {
  let test = ManagedBuffer::<TxContextRef>::new_from_bytes(b"Test");
}

使用的版本:

[dependencies.elrond-wasm]
version = "0.27.4"

[dev-dependencies.elrond-wasm-debug]
version = "0.27.4"
4

1 回答 1

1

在此文档部分的末尾附近提到“请记住,您不能在 execute_tx 函数之外创建托管类型。如果您需要这样做,您应该使用blockchain_wrapper.execute_in_managed_environment()

因此,创建 from bytes 的默认解决方案是在闭包ManagedBuffer内进行调用。blockchain_wrapper.execute_in_managed_environment()

另一种方法blockchain_wrapper.execute_in_managed_environment()是在调用let _ = DebugApi::dummy();之前调用ManagedBuffer::new_from_bytes。实例化在 Rust 测试中工作DebugApi::dummy()所需的模拟。ManagedBuffer::new_from_bytes

Ps:DebugApi::dummy()适用于静态字段。我没有检查过,但多次调用它可能会导致问题
Pps:谢谢马丁!

于 2022-02-15T21:33:53.303 回答