简体   繁体   中英

How can I set a caller in ink! contract unit testing function?

        fn do_check(
            &mut self
        ) -> Result<()> {
            let caller = self.env().caller();
            ...
        }

I am writing a test function for do_check function. Here, I want to set a caller but not sure how to do that.

#[cfg(test)]
    mod tests {
        use super::*;
        use ink_lang as ink;

        #[ink::test]
        fn do_check_works() {
            let mut test = Test::new();
            // here I want to set a caller for calling do_check
            test.do_check();
            ...

Please help me

You can set the caller using set_caller from ink_env :

let account = AccountId::from([0x1; 32]);
ink_env::test::set_caller::<ink_env::DefaultEnvironment>(account);

See the examples in the ink repo for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM