

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

# Test unitario con `aws-smithy-mocks` l' AWS SDK per Rust
<a name="testing-smithy-mocks"></a>

 AWS SDK per Rust Fornisce diversi approcci per testare il codice con cui interagisce. Servizi AWS Questo argomento descrive come utilizzare il [https://docs.rs/aws-smithy-mocks/latest/aws_smithy_mocks/](https://docs.rs/aws-smithy-mocks/latest/aws_smithy_mocks/)crate, che offre un modo semplice ma potente per simulare le risposte dei client AWS SDK a scopo di test.

## Panoramica di
<a name="overview-smithy-mock"></a>

Quando si scrivono test per il codice che utilizza Servizi AWS, spesso si desidera evitare di effettuare chiamate di rete effettive. The `aws-smithy-mocks` crate offre una soluzione che consente di:
+ Crea regole fittizie che definiscono come l'SDK deve rispondere a richieste specifiche.
+ Restituisce diversi tipi di risposte (successo, errore, risposte HTTP).
+ Abbina le richieste in base alle loro proprietà.
+ Definisci sequenze di risposte per testare il comportamento dei tentativi.
+ Verifica che le tue regole siano state utilizzate come previsto.

## Aggiungere la dipendenza
<a name="dependency-smithy-mock"></a>

In un prompt dei comandi per la directory del progetto, aggiungi la [https://crates.io/crates/aws-smithy-mocks](https://crates.io/crates/aws-smithy-mocks)cassa come dipendenza:

```
$ cargo add --dev aws-smithy-mocks
```

L'utilizzo dell'`--dev`[opzione](https://doc.rust-lang.org/cargo/commands/cargo-add.html) aggiunge la cassa alla `[dev-dependencies]` sezione del file. `Cargo.toml` Come [dipendenza di sviluppo](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#development-dependencies), non viene compilato e incluso nel file binario finale utilizzato per il codice di produzione.

Questo codice di esempio utilizza anche Amazon Simple Storage Service come Servizio AWS esempio e richiede funzionalità`test-util`.

```
$ cargo add aws-sdk-s3 --features test-util
```

In questo modo la cassa viene aggiunta alla `[dependencies]` sezione del `Cargo.toml` file.

## Utilizzo di base
<a name="basic-smithy-mocks"></a>

 Ecco un semplice esempio di come utilizzare per `aws-smithy-mocks` testare il codice che interagisce con Amazon Simple Storage Service (Amazon S3):

```
use aws_sdk_s3::operation::get_object::GetObjectOutput;
use aws_sdk_s3::primitives::ByteStream;
use aws_smithy_mocks::{mock, mock_client};

#[tokio::test]
async fn test_s3_get_object() {
    // Create a rule that returns a successful response
    let get_object_rule = mock!(aws_sdk_s3::Client::get_object)
        .then_output(|| {
            GetObjectOutput::builder()
                .body(ByteStream::from_static(b"test-content"))
                .build()
        });

    // Create a mocked client with the rule
    let s3 = mock_client!(aws_sdk_s3, [&get_object_rule]);

    // Use the client as you would normally
    let result = s3
        .get_object()
        .bucket("test-bucket")
        .key("test-key")
        .send()
        .await
        .expect("success response");

    // Verify the response
    let data = result.body.collect().await.expect("successful read").to_vec();
    assert_eq!(data, b"test-content");

    // Verify the rule was used
    assert_eq!(get_object_rule.num_calls(), 1);
}
```

## Creazione di regole fittizie
<a name="creating-rules-smithy-mocks"></a>

Le regole vengono create utilizzando la `mock!` macro, che accetta come argomento un'operazione client. È quindi possibile configurare il comportamento della regola. 

### Richieste corrispondenti
<a name="matching-requests-smithy-mocks"></a>

 Puoi rendere le regole più specifiche abbinando le proprietà su richiesta:

```
let rule = mock!(Client::get_object)
    .match_requests(|req| req.bucket() == Some("test-bucket") && req.key() == Some("test-key"))
    .then_output(|| {
        GetObjectOutput::builder()
            .body(ByteStream::from_static(b"test-content"))
            .build()
    });
```

### Diversi tipi di risposta
<a name="diff-response-smithy-mocks"></a>

Puoi restituire diversi tipi di risposte:

```
// Return a successful response
let success_rule = mock!(Client::get_object)
    .then_output(|| GetObjectOutput::builder().build());

// Return an error
let error_rule = mock!(Client::get_object)
    .then_error(|| GetObjectError::NoSuchKey(NoSuchKey::builder().build()));

// Return a specific HTTP response
let http_rule = mock!(Client::get_object)
    .then_http_response(|| {
        HttpResponse::new(
            StatusCode::try_from(503).unwrap(),
            SdkBody::from("service unavailable")
        )
    });
```

## Verifica del comportamento dei tentativi
<a name="testing-retry-behavior-smithy-mocks"></a>

Una delle funzionalità più potenti di `aws-smithy-mocks` è la possibilità di testare il comportamento dei tentativi di ripetizione definendo sequenze di risposte:

```
// Create a rule that returns 503 twice, then succeeds
let retry_rule = mock!(aws_sdk_s3::Client::get_object)
    .sequence()
    .http_status(503, None)                          // First call returns 503
    .http_status(503, None)                          // Second call returns 503
    .output(|| GetObjectOutput::builder().build())   // Third call succeeds
    .build();

// With repetition using times()
let retry_rule = mock!(Client::get_object)
    .sequence()
    .http_status(503, None)
    .times(2)                                        // First two calls return 503
    .output(|| GetObjectOutput::builder().build())   // Third call succeeds
    .build();
```

## Modalità di regole
<a name="rule-modes-smithy-mocks"></a>

È possibile controllare il modo in cui le regole vengono abbinate e applicate utilizzando le modalità di regola:

```
// Sequential mode: Rules are tried in order, and when a rule is exhausted, the next rule is used
let client = mock_client!(aws_sdk_s3, RuleMode::Sequential, [&rule1, &rule2]);

// MatchAny mode: The first matching rule is used, regardless of order
let client = mock_client!(aws_sdk_s3, RuleMode::MatchAny, [&rule1, &rule2]);
```

## Esempio: verifica del comportamento dei tentativi
<a name="example-retry-smithy-mocks"></a>

Ecco un esempio più completo che mostra come testare il comportamento dei nuovi tentativi:

```
use aws_sdk_s3::operation::get_object::GetObjectOutput;
use aws_sdk_s3::config::RetryConfig;
use aws_sdk_s3::primitives::ByteStream;
use aws_smithy_mocks::{mock, mock_client, RuleMode};

#[tokio::test]
async fn test_retry_behavior() {
    // Create a rule that returns 503 twice, then succeeds
    let retry_rule = mock!(aws_sdk_s3::Client::get_object)
        .sequence()
        .http_status(503, None)
        .times(2)
        .output(|| GetObjectOutput::builder()
            .body(ByteStream::from_static(b"success"))
            .build())
        .build();

    // Create a mocked client with the rule and custom retry configuration
    let s3 = mock_client!(
        aws_sdk_s3,
        RuleMode::Sequential,
        [&retry_rule],
        |client_builder| {
            client_builder.retry_config(RetryConfig::standard().with_max_attempts(3))
        }
    );

    // This should succeed after two retries
    let result = s3
        .get_object()
        .bucket("test-bucket")
        .key("test-key")
        .send()
        .await
        .expect("success after retries");

    // Verify the response
    let data = result.body.collect().await.expect("successful read").to_vec();
    assert_eq!(data, b"success");

    // Verify all responses were used
    assert_eq!(retry_rule.num_calls(), 3);
}
```

## Esempio: risposte diverse in base ai parametri della richiesta
<a name="example-request-param-smithy-mocks"></a>

Puoi anche creare regole che restituiscono risposte diverse in base ai parametri della richiesta:

```
use aws_sdk_s3::operation::get_object::{GetObjectOutput, GetObjectError};
use aws_sdk_s3::types::error::NoSuchKey;
use aws_sdk_s3::Client;
use aws_sdk_s3::primitives::ByteStream;
use aws_smithy_mocks::{mock, mock_client, RuleMode};

#[tokio::test]
async fn test_different_responses() {
    // Create rules for different request parameters
    let exists_rule = mock!(Client::get_object)
        .match_requests(|req| req.bucket() == Some("test-bucket") && req.key() == Some("exists"))
        .sequence()
        .output(|| GetObjectOutput::builder()
            .body(ByteStream::from_static(b"found"))
            .build())
        .build();

    let not_exists_rule = mock!(Client::get_object)
        .match_requests(|req| req.bucket() == Some("test-bucket") && req.key() == Some("not-exists"))
        .sequence()
        .error(|| GetObjectError::NoSuchKey(NoSuchKey::builder().build()))
        .build();

    // Create a mocked client with the rules in MatchAny mode
    let s3 = mock_client!(aws_sdk_s3, RuleMode::MatchAny, [&exists_rule, &not_exists_rule]);

    // Test the "exists" case
    let result1 = s3
        .get_object()
        .bucket("test-bucket")
        .key("exists")
        .send()
        .await
        .expect("object exists");

    let data = result1.body.collect().await.expect("successful read").to_vec();
    assert_eq!(data, b"found");

    // Test the "not-exists" case
    let result2 = s3
        .get_object()
        .bucket("test-bucket")
        .key("not-exists")
        .send()
        .await;

    assert!(result2.is_err());
    assert!(matches!(result2.unwrap_err().into_service_error(),
                    GetObjectError::NoSuchKey(_)));
}
```

## Best practice
<a name="best-practices-smithy-mocks"></a>

Quando si utilizza `aws-smithy-mocks` per il test:

1.  Corrisponde a richieste specifiche: utilizza `match_requests()` questa opzione per garantire che le regole si applichino solo alle richieste previste, in particolare con`RuleMode:::MatchAny`.

1.  Verifica l'utilizzo delle regole: verifica `rule.num_calls()` che le regole siano state effettivamente utilizzate.

1.  Gestione degli errori di test: crea regole che restituiscano errori per verificare come il codice gestisce gli errori.

1.  Verifica la logica dei tentativi: utilizza le sequenze di risposta per verificare che il codice gestisca correttamente eventuali classificatori di tentativi personalizzati o altri comportamenti di ripetizione dei tentativi.

1. Mantieni i test concentrati: crea test separati per scenari diversi anziché cercare di coprire tutto in un unico test.