Icon HelpCircleForumIcon Link

⌘K

Icon HelpCircleForumIcon Link
Logging in Rust Tests

Icon LinkLogging in Rust tests

Icon LinkGenerating a Test Template in Rust

To create your own test template using Rust, follow these steps with cargo-generate in the script project directory:

  1. Install cargo-generate:
cargo install cargo-generate --locked
  1. Generate the template:
cargo generate --init fuellabs/sway templates/sway-test-rs --name sway-store

Icon LinkLogging

We previously covered imports and setting up the predicate in an earlier introduction to Sway tutorial, specifically in the Rust testing section Icon Link. If you haven't checked that out yet, I highly recommend doing so.

Copy and paste the rust test below:

use fuels::{
    prelude::*,
    accounts::fuel_crypto::SecretKey
};
 
abigen!(Script(
    name = "MultiSigScript",
    abi = "./out/debug/predicate-script-logging-abi.json"
));
 
#[tokio::test]
async fn script_logs() -> Result<()> {
    // WALLET
    let private_key: SecretKey =
    "0xc2620849458064e8f1eb2bc4c459f473695b443ac3134c82ddd4fd992bd138fd"
        .parse()
        .unwrap();
 
    let mut wallet: WalletUnlocked = WalletUnlocked::new_from_private_key(private_key, None);
 
    // TOKENS
 
    let all_coins = [&wallet]
        .iter()
        .flat_map(|wallet| {
            setup_single_asset_coins(wallet.address(), AssetId::default(), 10, 1_000_000)
        })
        .collect::<Vec<_>>();
 
    // PROVIDER
    let node_config = Config::default();
 
    let provider = setup_test_provider(all_coins, vec![], Some(node_config), None).await.unwrap();
 
    [&mut wallet]
        .iter_mut()
        .for_each(|wallet| {
            wallet.set_provider(provider.clone());
        });
 
    let bin_path = "./out/debug/predicate-script-logging.bin";
 
    let instance = MultiSigScript::new(wallet.clone(), bin_path);
 
    let response = instance.main().call().await?;
    
    let logs = response.decode_logs();
    println!("{:?}", logs);
    Ok(())
    // Now you have an instance of your contract you can use to test each function
}

Now, I want to draw your attention to a specific portion of the code here:

let bin_path = "./out/debug/predicate-script-logging.bin";
 
let instance = MultiSigScript::new(wallet.clone(), bin_path);
 
let response = instance.main().call().await?;
 
let logs = response.decode_logs();
println!("{:?}", logs);

We can now call decode_logs to extract our secret number, something we weren't able to do when testing with predicates.

To enable print outputs to appear in the console during tests, you can use the nocapture flag.

cargo test -- --nocapture

Remembering this method is essential when developing more complex predicates, especially as debugging becomes increasingly challenging.