问题描述
使用ink3“ self.env()。block_timestamp()”的正确方法是什么
参考代码段(完整代码为@ https://gist.github.com/shamb0/a1f24cd7981e169cc5b7d1e1b3ec4dd4)
pub fn get_bts(&self) -> u64 {
let bts = self.env().block_timestamp();
bts
}
对此功能调用测试执行会导致错误在“未初始化的执行上下文:UninitializedBlocks”中惊恐。
完整的控制台跟踪
cargo +nightly test -- --nocapture
Finished test [unoptimized + debuginfo] target(s) in 0.04s
Running target/debug/deps/ink3_spad-ae1376ecf8f75a7e
running 3 tests
test ink3_spad::tests::default_works ... thread 'ok
ink3_spad::tests::bts_works' panicked at 'uninitialized execution context: UninitializedBlocks',$HOME/rusthome/.cargo/registry/src/github.com-1ecc6299db9ec823/ink_env-3.0.0-rc2/src/engine/off_chain/impls.rs:277:14test ink3_spad::tests::it_works ...
oknote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test ink3_spad::tests::bts_works ... FAILED
failures:
failures:
ink3_spad::tests::bts_works
test result: FAILED. 2 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
error: test failed,to rerun pass '--lib'
解决方法
注释必须为“#[ink :: test]”,而不是#[test]
#[ink::test]
fn bts_works() {
let ink3_spad = Ink3Spad::new(false);
assert_eq!(ink3_spad.get(),false);
let dbg_fmtstr = format!("{:?}",ink3_spad.get_bts() );
ink_env::debug_println( &dbg_fmtstr );
}
感谢@LaurentTrk为我指出解决方案...