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

Time:11-05

        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

CodePudding user response:

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.

  • Related