From 86e7d85b02bd5749243b955c2d06621decf26b59 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Tue, 24 Sep 2024 12:01:10 +0700 Subject: [PATCH] fix: expect return error method --- pop-api/examples/fungibles/tests.rs | 6 +++++- pop-api/examples/fungibles/utils.rs | 9 ++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pop-api/examples/fungibles/tests.rs b/pop-api/examples/fungibles/tests.rs index 33ee18bd..57e93005 100644 --- a/pop-api/examples/fungibles/tests.rs +++ b/pop-api/examples/fungibles/tests.rs @@ -268,15 +268,19 @@ fn transfer_fails_with_insufficient_balance( const AMOUNT: Balance = TOKEN_MIN_BALANCE * 4; assert_ok!(session.sandbox().mint_into(&TOKEN_ID, &contract.clone(), AMOUNT)); assert_ok!(session.sandbox().mint_into(&TOKEN_ID, &BOB, AMOUNT)); + assert_eq!(session.sandbox().balance_of(&TOKEN_ID, &contract.clone()), AMOUNT); session.set_actor(contract.clone()); // Failed with `InsufficientBalance`. + let data = serde_json::to_string::<[u8; 0]>(&[]).unwrap(); expect_call_reverted( &mut session, TRANSFER, - vec![BOB.to_string(), (AMOUNT + 1).to_string()], + vec![BOB.to_string(), (AMOUNT + 1).to_string(), data], PSP22Error::InsufficientBalance, ); + // Check that balance of account is not changed. + assert_eq!(session.sandbox().balance_of(&TOKEN_ID, &contract), AMOUNT); Ok(()) } diff --git a/pop-api/examples/fungibles/utils.rs b/pop-api/examples/fungibles/utils.rs index 44755f8b..fb6a1e3f 100644 --- a/pop-api/examples/fungibles/utils.rs +++ b/pop-api/examples/fungibles/utils.rs @@ -39,7 +39,7 @@ pub(super) fn last_contract_event(session: &Session) -> Option> session.record().last_event_batch().contract_events().last().cloned() } -/// Execute a contract method and exepct CallReverted error to be returned. +/// Execute a contract method and expect CallReverted error to be returned. pub(super) fn expect_call_reverted( session: &mut Session, function: &str, @@ -47,8 +47,11 @@ pub(super) fn expect_call_reverted( err: PSP22Error, ) { let call = session.call::(function, ¶ms, None); - if let Err(SessionError::CallReverted(error)) = call { - assert_eq!(error[1..], Err::<(), PSP22Error>(err).encode()); + match call { + Err(SessionError::CallReverted(error)) => { + assert_eq!(error[1..], Err::<(), PSP22Error>(err).encode()) + }, + _ => panic!("Expect call reverted"), } }