Instelling
Voordat u uw programma test, zorg ervoor dat u:
- Bouw uw programma om het bestand
.sote genereren - Voeg
litesvmtoe als een dev-afhankelijkheid aan het bestandCargo.toml - Voeg eventuele aanvullende dev-afhankelijkheden toe die mogelijk nodig zijn,
zoals
solana-sdkenlitesvm-token - Maak een testbestand aan in uw map
tests
Basisindeling voor programmatest
Hier zijn alle basisstappen die nodig zijn om een instructie aan te roepen en uit te voeren in een Solana-programma.
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!");}
De standaard testomgeving die wordt gegenereerd met LiteSVM::new() bevat alle
ingeschakelde runtime-functies, standaard sysvars, precompiles, spl-programma's,
sigverify en alle ingebouwde programma's, zoals het System Program.
Als u wilt communiceren met een programma dat niet is opgenomen in de standaard testomgeving, moet u dat programma implementeren in de testomgeving.
Volgende stappen
In het volgende gedeelte leren we alles over het implementeren van programma's in de litesvm testomgeving.
Is this page helpful?