pub trait TXResultAssertions {
// Required methods
fn fails(self) -> Result<TXErrorAssertions>;
fn succeeds(self) -> Result<TXSuccessAssertions>;
}Expand description
Extension trait for transaction results providing assertion methods.
This trait adds convenient assertion methods to TXResult for testing
whether transactions succeed or fail as expected.
Required Methods§
Sourcefn fails(self) -> Result<TXErrorAssertions>
fn fails(self) -> Result<TXErrorAssertions>
Asserts that the transaction fails, converting a successful transaction to an error.
This method is used in tests to verify that a transaction is expected to fail.
It returns a TXErrorAssertions struct that provides additional assertion methods
for checking specific error conditions.
§Returns
Returns Ok(TXErrorAssertions) if the transaction failed as expected, or an error
if the transaction unexpectedly succeeded.
§Example
// Try to mint tokens from unauthorized user (should fail)
let mint_ix = anchor_spl::token::spl_token::instruction::mint_to(
&anchor_spl::token::ID,
&mint.key,
&user_ata.key,
&unauthorized_user.pubkey(), // Wrong authority!
&[],
1_000_000,
)?;
// Test that unauthorized minting fails
let result = env.execute_ixs_with_signers(&[mint_ix], &[&unauthorized_user]);
result
.fails()? // Assert the transaction fails
.with_custom_error(4)?; // SPL Token error: OwnerMismatchSourcefn succeeds(self) -> Result<TXSuccessAssertions>
fn succeeds(self) -> Result<TXSuccessAssertions>
Asserts that the transaction succeeds, converting a failed transaction to an error.
This method is used in tests to verify that a transaction is expected to succeed.
It returns a TXSuccessAssertions struct that contains the successful transaction
metadata for further validation if needed.
§Returns
Returns Ok(TXSuccessAssertions) if the transaction succeeded as expected, or an error
if the transaction unexpectedly failed.
§Example
// Mint tokens from the authorized owner (should succeed)
let mint_ix = anchor_spl::token::spl_token::instruction::mint_to(
&anchor_spl::token::ID,
&mint.key,
&owner_ata.key,
&owner.pubkey(), // Correct authority
&[],
1_000_000,
)?;
// Test that authorized minting succeeds
let result = env.execute_ixs_with_signers(&[mint_ix], &[&owner]);
let assertions = result.succeeds()?;
// Can access transaction metadata
println!("Used {} compute units", assertions.compute_units());