test for init function from examples doesn't works

186 views Asked by At

When I run code from the official repo I got the next error:

Error executing ExecutionError: ExecutionError { inner:
ExecutionErrorInner { kind: SuiMoveVerificationError, source: 
Some("0000000000000000000000000000000000000000::exp01::init at 
offset 9. Cannot call a module's 'init' function from another
 Move function") } }

source

1

There are 1 answers

0
Frank C. On

Yes, they need to fix the repo as changes with 0.16.0 removed ability to call init from test modules.

The workaround is to factor out the steps in the current init to one or more helper functions. That way, a test_init function can mirror what init does and you can call the test_init from test modules.

Before

    /// Initialize new deployment
    fun init(ctx: &mut TxContext) {
        // Do A
        // Do B
        // Do C
    }

   #[test_only]
    /// Wrapper of module initializer for testing
    public fun test_init(ctx: &mut TxContext) {
        init(ctx)
    }

After


    fun do_a(ctx: &mut TxContext) {
        // Refactored from init
    }

    fun do_b(ctx: &mut TxContext) {
        // Refactored from init
    }

    /// Initialize new deployment
    fun init(ctx: &mut TxContext) {
        do_a(ctx);
        do_b(ctx)
    }

   #[test_only]
    /// Wrapper of module initializer for testing
    public fun test_init(ctx: &mut TxContext) {
        do_a(ctx);
        do_b(ctx)
    }