ios - swift 中包含 AWSAPIGatewayResponse 的单元测试方法

标签 ios swift amazon-web-services unit-testing aws-api-gateway

我有以下方法,我正在尝试在 iOS/Swift 中对其进行单元测试

func apiResponseResults(response: AWSAPIGatewayResponse, sessionObject: Session) {
    if response.statusCode == 200 {
        // code
    } else {
        // code
    }
}

收到响应时,从我的 api invoke 方法调用此方法。我想通过创建一个假的 AWSAPIGatewayResponse 对象来对此进行单元测试。 我无法创建 AWSAPIGatewayResponse 对象。有没有办法在我的单元测试类中初始化这个对象。我只想创建一个 AWSAPIGatewayResponse 对象,其 statusCode 为 200300。如有任何帮助,我们将不胜感激。

最佳答案

Pradeep ,欢迎来到 StackOverflow。 👋

您可以使用 AWSAPIGatewayResponse 中您感兴趣的功能子集定义一个协议(protocol),使 AWSAPIGatewayResponse 符合该协议(protocol),并使 apiResponseResults(response:, session:) 期望该类型的值。

protocol Response {
  var statusCode: Int { get }
}

extension AWSAPIGatewayResponse: Response { }

func apiResponseResults(response: Response, sessionObject: Session) { ... }

在您的测试中,您可以定义符合Response的测试替身,并将其用作方法的输入参数。

struct TestResponse: Response {
  let statusCode: Int
}

// Use it like this
let response = TestResponse(statusCode: 200)

希望这有帮助。

关于ios - swift 中包含 AWSAPIGatewayResponse 的单元测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55741506/

相关文章:

ios - 选择 UITableViewCell 时,UIButton 被覆盖

ios - 动画 subview

ios - 什么是 iOS Home App URL Scheme

Linux 命令行搜索和替换

amazon-web-services - 在 s3 中启用版本控制的成本

ios - 带有图像和文本的 UIButton 可能吗?

iOS 10,Swift 3 API 调用 SSL 错误

ios - float 在 UIView 上的水平 ScrollView 而不是 Paging

ios - CoreLocation 捕获速度

amazon-web-services - 您无法在组织初始化时向其添加帐户。稍后再试