1

Sorry for the title but I don't know how to explain it better.

I have this simple custom pallet and I try to test the create_random_number dispatchable function with a simple assert_ok! but I get this error from the terminal:

running 2 tests
test mock::__construct_runtime_integrity_test::runtime_integrity_tests ... ok
test tests::crate_random_number ... FAILED

failures:

---- tests::crate_random_number stdout ----
thread 'tests::crate_random_number' panicked at 'attempt to subtract with overflow', /rustc/263edd43c5255084292329423c61a9d69715ebfa/library/core/src/ops/arith.rs:240:1

The test panic at this line but I don't understand why because if I try to cargo build --release and call this function on the polkadot app it works fine.

1 Answer 1

2

This is caused by the fact that the block-number is 0 when your test runs.
The coinflip pallet then tries to calculate 0-1 here.
You can set the block-number to 1 in your new_test_ext function like this:

pub fn new_test_ext() -> sp_io::TestExternalities {
    let mut ext: sp_io::TestExternalities = system::GenesisConfig::default().build_storage::<Test>().unwrap().into();
    ext.execute_with(|| System::set_block_number(1));
    ext
}

Many tests do that, for similar reasons, so its not a red-flag.

1
  • 1
    Thank you for the explanation! Now it works fine :)
    – viac92
    Aug 30, 2022 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.