sui move unit test学习

环境配置sui1.24核心概念sui的scenario创建,并且运行,测试运行脚本suimovetest运行代码moduletest_learning::Sword{publicstructSwordhaskey,store{id:U

环境配置sui 1.24

核心概念 sui的scenario创建,并且运行,测试

运行脚本

sui move test

运行代码

module test_learning::Sword {

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

    public struct AdminCap has key, store {
        id: UID, 
    }

    public fun sword_create(magic: u64, strength: u64, ctx: &mut TxContext): Sword{
        let sword = Sword {
            id: object::new(ctx),
            magic: magic,
            strength: strength,
        };
        sword
    }

    #[test] 
    public fun test_create_sowrd() {
        use sui::test_scenario;
        let addr1 = @0xA;
        let addr2 = @0xB;

        let mut scenario = test_scenario::begin(addr1);
        {
            let sword = sword_create(47, 10, scenario.ctx());
            transfer::public_transfer(sword, addr1)
        };

        scenario.next_tx(addr1);
        {
            let sword = scenario.take_from_sender<Sword>();       
            transfer::public_transfer(sword, addr2);
        };

        scenario.next_tx(addr2);
        {
            let sword = scenario.take_from_sender<Sword>();

            assert!(sword.magic == 47, 1);

            scenario.return_to_sender(sword)
        };

        scenario.end();
    }
}
点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
用户_18921
用户_18921
0xa10f...9ab5
江湖只有他的大名,没有他的介绍。