Configurazione
Prima di testare il tuo programma, assicurati di:
- Compilare il tuo programma per generare il file
.so - Aggiungere
litesvmcome dipendenza di sviluppo al fileCargo.toml - Aggiungere eventuali dipendenze di sviluppo aggiuntive che potrebbero essere
necessarie, come
solana-sdkelitesvm-token - Creare un file di test nella directory
tests
Struttura Base del Test del Programma
Ecco tutti i passaggi fondamentali necessari per richiamare ed eseguire un'istruzione in un programma Solana.
tests/program_test.rs
use litesvm::LiteSVM;use solana_sdk::{instruction::{AccountMeta, Instruction},pubkey::Pubkey,signature::{Keypair, Signer},transaction::Transaction,};use my_program;#[test]fn test_my_program() {// Initialize the test environmentlet mut svm = LiteSVM::new();// Deploy your program to the test environmentlet program_id = Pubkey::from(my_program::ID);let program_bytes = include_bytes!("../../../target/deploy/my_program.so");svm.add_program(program_id, program_bytes);// Create and fund test accountslet payer = Keypair::new();svm.airdrop(&payer.pubkey(), 10_000_000_000).unwrap();// Create your instructionlet instruction = Instruction {program_id,accounts: vec![AccountMeta::new(payer.pubkey(), true),],data: vec![],};// Build transactionlet tx = Transaction::new_signed_with_payer(&[instruction],Some(&payer.pubkey()),&[&payer],svm.latest_blockhash(),);// Send transactionlet result = svm.send_transaction(tx).unwrap();// Check transaction succeededprintln!("Transaction logs: {:?}", result.logs);println!("Program executed successfully!");}
L'ambiente di test predefinito generato con LiteSVM::new() include tutte le
funzionalità runtime abilitate, sysvars predefinite, precompilazioni, programmi
spl, verifica delle firme e tutti i programmi integrati, come il System Program.
Se desideri interagire con qualsiasi programma non incluso nell'ambiente di test predefinito, devi distribuire quel programma nell'ambiente di test.
Prossimi Passi
Nella prossima sezione, impareremo tutto su come distribuire i programmi nell'ambiente di test litesvm.
Is this page helpful?