I'm trying to create a new ManagedBuffer
in a rust test, but the tests panic. I've tried using both, the ManagedBuffer::new_from_bytes
function and the managed_buffer!
macro, but both of them result in the same error:
thread 'use_managed_buffer_new_from_bytes' panicked at 'called
Option::unwrap()
on aNone
value', /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
thread 'use_managed_buffer_macro' panicked at 'called
Option::unwrap()
on aNone
value', /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");
}
Versions used:
[dependencies.elrond-wasm]
version = "0.27.4"
[dev-dependencies.elrond-wasm-debug]
version = "0.27.4"
Close to the end of this docs section it's mentioned that "Keep in mind you can't create managed types outside of the execute_tx functions. If you need to do that, you should use blockchain_wrapper.execute_in_managed_environment()
"
So, the default solution to creating a ManagedBuffer
from bytes is to make the call inside a blockchain_wrapper.execute_in_managed_environment()
closure.
An alternative to blockchain_wrapper.execute_in_managed_environment()
is to call let _ = DebugApi::dummy();
before calling ManagedBuffer::new_from_bytes
. DebugApi::dummy()
instantiates the required mocks for the ManagedBuffer::new_from_bytes
to work in Rust tests.
P.s.: DebugApi::dummy()
works with static fields. I've not checked but calling it multiple times might result in issues
P.p.s: thank you Martin!