'Check balance of individual accounts in unit tests

I can't figure out how to test the balance of given accounts when writing unit tests.

For example, Bob buys something. He pays 10 NEAR. 3 NEAR should go to alice.near, 7 NEAR should go to bob.near. I want to check if the 3 and the 7 NEAR are there. I'm using VMContext. I tried switching the context, so I can use env::account_balance(), but that will not solve my problem.

let mut context = get_context(600_000_000_000_000_000_000_000);                                                       // Alice is person who interacts
        testing_env!(context.build());
        
        let mut contract = Contract::new_default_meta(to_valid_account("vault.near"), to_valid_account("carol.near"));       // Vault is owner, Carol will be admin
        let price = U128(500_000_000_000_000_000_000_000);
        
        contract.mint_root(test_token_metadata(), to_valid_account("carol.near"), price.clone(), None,
            Some(HashMap::from([                                                                                            // Alice: 20%
                (to_valid_account("alice.near"), 2000),                                                                     // Vault: 80%
                (to_valid_account("vault.near"), 8000)
                ]))
            );
        
        log!("caller: {:?}", env::current_account_id());
        log!("Alice balance: {:?}", env::account_balance());
        context.predecessor_account_id(to_valid_account("bob.near"));                                                       // Bob interacts with the contract
        context.signer_account_id(to_valid_account("bob.near"));
        //testing_env!(context.build());
        contract.buy_nft_from_vault("fono-root-0-0".to_string());

        context.predecessor_account_id(to_valid_account("alice.near"));                                                       // Bob interacts with the contract
        context.signer_account_id(to_valid_account("alice.near"));
        log!("caller: {:?}", env::current_account_id());
        log!("Alice balance: {:?}", env::account_balance());

I'm looking for a function that looks something like this: context_get_balance("alice.near"); And it would give me the balance for alice.near in the simulated blockchain. Is it possible to do this somehow? Maybe it is not possible to do this. If not, what is the best approach when writing similar unit tests?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source