swift - 如何测试快速发出 AWSMobileClient 请求的函数

标签 swift amazon-web-services unit-testing testing

我有一个函数

final class API {
  func forgotPassword(
    username: String
  ) -> Future<ConfirmationStatus, Never> {
    Future { _ in
      AWSMobileClient.default().forgotPassword(
        username: username
      ) { forgotPasswordResult, error in
        if let forgotPasswordResult = forgotPasswordResult {
          switch forgotPasswordResult.forgotPasswordState {
          case .confirmationCodeSent:
            // show an alert to a user here
            break
          default:
            logger.error("Error: Invalid case.")
          }
        } else if let error = error {
          logger.error("Error occurred: \(error.localizedDescription)")
        }
      }
    }.onFailure {
      logger.error($0)
    }
  }
}

当用户点击按钮时将被调用。我想测试一下。我大致了解我应该如何测试它。我应该创建一个将被调用而不是 AWSMobileClient.default().forgotPassword() 的函数,我应该在 forgotPassword 测试中使用它。 它可能在协议(protocol)的扩展中起作用。而 AWSMobileClient 应该符合这个协议(protocol)。

我应该如何测试 API.forgotPassword

我用了BrightFuture发出异步请求。

最佳答案

应该有允许依赖注入(inject)的协议(protocol)。

让它成为AuthClient。它应该具有我们将从 AWSMobileClient 使用的所有属性。并声明一个扩展。

extension AWSMobileClient: AuthClient {}

我们应该有一个符合这个协议(protocol)的类。例如 Auth。它应该有一个 authClient 属性。关心我们的身份验证客户端将是可变的。

在应用顶部的某个地方,我们将进行 Auth 初始化,将 ASWMobileClient 作为 authClient

Auth(AWSMobileClient.default())

在测试中,我们应该将模拟传递给 AuthMockAuthClient:AuthClient 将包含我们要在应用中测试和使用的所有方法。

关于swift - 如何测试快速发出 AWSMobileClient 请求的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59337666/

相关文章:

amazon-web-services - Amazon ItemLookup API - 缺少参数操作

c# - 为什么以下测试使用 Mbunit 和 Rhino 模拟失败?

ios - Swift: TableView 父类(super class)错误

python - Django/报告实验室 : Save generated pdf directly to FileField in AWS s3

ios - 在 Swift 中动画循环进度 View

amazon-web-services - 指向 S3 存储桶和 ELB 的 Cloudfront 分发

java - 如何使用 AssertJ 检查 boolean 值 getter?

java - 使用 Mockito,如果我使用 @InjectMocks 将一个对象注入(inject)到我的模拟中,如何避免清空其他对象?

swift - Firebase Twitter oAuth 回调不适用于 Swift ios13

macos - 如何在 API 结果中调用子类?