sui icon indicating copy to clipboard operation
sui copied to clipboard

test_scenario is not working for module initialization

Open xstarjx opened this issue 2 years ago • 4 comments

After upgrading sui to 0.16.0 from 0.15.2, I cannot init module using the tutorial function test_sword_transactions. Source code:

module suiso::my_module {
    // Part 1: imports
    use sui::object::{Self, UID};
    use sui::transfer;
    use sui::tx_context::{Self, TxContext};

    struct Sword has key, store {
        id: UID,
        magic: u64,
        strength: u64,
    }

    struct Forge has key, store {
        id: UID,
        swords_created: u64,
    }

    fun init(ctx: &mut TxContext) {
        let forge = Forge {
            id: object::new(ctx),
            swords_created: 0,
        };
        transfer::transfer(forge, tx_context::sender(ctx));
    }

    public fun magic(self: &Sword): u64 {
        self.magic
    }

    public fun strength(self: &Sword): u64 {
        self.strength
    }

    public entry fun sword_create(forge: &mut Forge, magic: u64, strength: u64, recipient: address, ctx: &mut TxContext) {
        use sui::transfer;

        // create a sword
        let sword = Sword {
            id: object::new(ctx),
            magic: magic,
            strength: strength,
        };
        forge.swords_created = forge.swords_created + 1;
        // transfer the sword
        transfer::transfer(sword, recipient);
    }

    public fun destroy_sword(self: Sword) {
        let Sword {
            id,
            magic: _magic,
            strength: _strength
        } = self;
        object::delete(id);
    }

    public fun swords_created(self: &Forge): u64 {
        self.swords_created
    }

    // part 5: public/ entry functions (introduced later in the tutorial)
    // part 6: private functions (if any)
    #[test]
    fun test_sword_transactions() {
        use sui::test_scenario;

        // create test addresses representing users
        let admin = @0xBABE;
        let initial_owner = @0xCAFE;
        let final_owner = @0xFACE;

        // first transaction to emulate module initialization
        let scenario_val = test_scenario::begin(admin);
        let scenario = &mut scenario_val;
        {
            init(test_scenario::ctx(scenario));
        };
        // second transaction executed by admin to create the sword
        test_scenario::next_tx(scenario, admin);
        {
            let forge = test_scenario::take_from_sender<Forge>(scenario);
            // create the sword and transfer it to the initial owner
            sword_create(&mut forge, 42, 7, initial_owner, test_scenario::ctx(scenario));
            test_scenario::return_to_sender(scenario, forge)
        };
        // third transaction executed by the initial sword owner
        test_scenario::next_tx(scenario, initial_owner);
        {
            // extract the sword owned by the initial owner
            let sword = test_scenario::take_from_sender<Sword>(scenario);
            // transfer the sword to the final owner
            transfer::transfer(sword, final_owner);
        };
        // fourth transaction executed by the final sword owner
        test_scenario::next_tx(scenario, final_owner);
        {

            // extract the sword owned by the final owner
            let sword = test_scenario::take_from_sender<Sword>(scenario);
            // verify that the sword has expected properties
            assert!(magic(&sword) == 42 && strength(&sword) == 7, 1);
            // return the sword to the object pool (it cannot be simply "dropped")
            test_scenario::return_to_sender(scenario, sword)
        };
        test_scenario::end(scenario_val);
    }
}

Error code: image

xstarjx avatar Nov 18 '22 05:11 xstarjx

After doing the upgrade, I get the same error message when trying to run any of the example tests.

ckeyter avatar Nov 18 '22 14:11 ckeyter

Has anyone found the solution or an alternative way?

xiyu1984 avatar Nov 21 '22 14:11 xiyu1984

@xiyu1984 https://stackoverflow.com/questions/74513153/test-for-init-function-from-examples-doesnt-works/74516843#74516843 here

But it is an awkward solution, and this code smells. I am waiting for the fix of the bug.

ChzenChzen avatar Nov 21 '22 17:11 ChzenChzen

I think the solution recommended is the stack overflow post is the way to go. Although it would be convenient to be able to call init() directly from suiso's tests, this won't be possible if someone writes a module that depends on suiso and needs to write a test that depends on suiso::init(). To enable that future dev to test properly, you need the #[test_only] public init_for_testing wrapper

sblackshear avatar Nov 23 '22 21:11 sblackshear

Apologies for this regression--we've landed https://github.com/MystenLabs/sui/pull/6435 and cherrypicked it to the devnet branch. Reinstalling your local devtools with cargo install --locked --git https://github.com/MystenLabs/sui.git --branch "devnet" sui should give you the old behavior for now, and we'll work on a better fix for the next release

sblackshear avatar Nov 29 '22 00:11 sblackshear