

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在适用于 Rust 的 AWS SDK `aws-smithy-mocks` 中进行单元测试
<a name="testing-smithy-mocks"></a>

 适用于 Rust 的 AWS SDK 提供了多种方法来测试与 AWS 服务之交互的代码。本主题介绍了如何使用 [https://docs.rs/aws-smithy-mocks/latest/aws_smithy_mocks/](https://docs.rs/aws-smithy-mocks/latest/aws_smithy_mocks/) crate，它提供了一种简单而强大的方法来模拟 AWS SDK 客户端响应以用于测试目的。

## 概述
<a name="overview-smithy-mock"></a>

在为使用的代码编写测试时 AWS 服务，您通常希望避免进行实际的网络调用。`aws-smithy-mocks` crate 提供了一种解决方案，支持：
+ 创建 mock 规则，用于定义 SDK 应如何响应特定请求。
+ 返回不同类型的响应（成功、错误、HTTP 响应）。
+ 根据请求的属性匹配请求。
+ 定义用于测试重试行为的响应序列。
+ 验证规则是否按预期使用。

## 添加依赖项
<a name="dependency-smithy-mock"></a>

在项目目录的命令提示符中，将 [https://crates.io/crates/aws-smithy-mocks](https://crates.io/crates/aws-smithy-mocks) crate 添加为依赖项：

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

使用 `--dev` [选项](https://doc.rust-lang.org/cargo/commands/cargo-add.html)可将 crate 添加到 `Cargo.toml` 文件的 `[dev-dependencies]` 部分。作为[开发依赖项](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#development-dependencies)，它不会被编译并包含在用于生产代码的最终二进制文件中。

此示例代码还使用 Amazon 简单存储服务作为示例 AWS 服务，并且需要功能`test-util`。

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

这会将 crate 添加到 `Cargo.toml` 文件的 `[dependencies]` 部分。

## 基本用法
<a name="basic-smithy-mocks"></a>

 以下是一个简单的示例，展示了如何使用 `aws-smithy-mocks` 测试与 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);
}
```

## 创建 mock 规则
<a name="creating-rules-smithy-mocks"></a>

规则是使用 `mock!` 宏创建的，该宏将客户端操作作为参数。然后，您可以配置规则的行为方式。

### 匹配请求
<a name="matching-requests-smithy-mocks"></a>

 您可以通过匹配请求属性来使规则更加具体：

```
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()
    });
```

### 不同的响应类型
<a name="diff-response-smithy-mocks"></a>

您可以返回不同类型的响应：

```
// 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")
        )
    });
```

## 测试重试行为
<a name="testing-retry-behavior-smithy-mocks"></a>

`aws-smithy-mocks` 最强大的功能之一是能够通过定义响应序列来测试重试行为：

```
// 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();
```

## 规则模式
<a name="rule-modes-smithy-mocks"></a>

您可以使用规则模式控制规则的匹配和应用方式：

```
// 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]);
```

## 示例：测试重试行为
<a name="example-retry-smithy-mocks"></a>

以下是一个更完整的示例，展示了如何测试重试行为：

```
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);
}
```

## 示例：基于请求参数的不同响应
<a name="example-request-param-smithy-mocks"></a>

您还可以创建规则，根据请求参数返回不同的响应：

```
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(_)));
}
```

## 最佳实践
<a name="best-practices-smithy-mocks"></a>

使用 `aws-smithy-mocks` 进行测试时：

1.  匹配特定请求：使用 `match_requests()` 确保规则仅应用于预期请求，尤其是使用 `RuleMode:::MatchAny`。

1.  验证规则使用情况：检查 `rule.num_calls()` 以确保规则已实际使用。

1.  测试错误处理：创建返回错误的规则，以测试代码是如何处理故障的。

1.  测试重试逻辑：使用响应序列来验证代码是否正确处理了任何自定义重试分类器或其他重试行为。

1. 保持测试的针对性：针对不同的场景创建单独的测试，而不是使用一个测试来测试所有内容。