-1

使用ink3“self.env().block_timestamp()”的正确方法是什么

参考代码片段(完整代码为@https ://gist.github.com/shamb0/a1f24cd7981e169cc5b7d1e1b3ec4dd4

        pub fn get_bts(&self) -> u64 {
            let bts = self.env().block_timestamp();
            bts
        }

在此函数上调用测试执行最终会在 'uninitialized execution context: 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'
4

1 回答 1

0

注释必须是“#[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 为我指出解决方案...

于 2020-10-24T15:58:25.827 回答