ios - 如何使用 Alamofire 和依赖注入构建适合测试的网络层?
问题描述
所以,我一直在尝试构建一个易于使用、维护和测试的网络层。我决定使用 Alamofire,因为它非常易于使用且易于维护(特别是在使用路由时)。
但是我一直卡在测试部分。原因如下:如何将基本 API url 注入路由器?我应该使用其他东西而不是路由模式吗?
public protocol Router: URLRequestConvertible {
var path: String { get }
var method: HTTPMethod { get }
var parameters: Parameters { get }
}
// this extension will provide a default implementation
// of the asURLRequest function
extension Router {
public func asURLRequest() throws -> URLRequest {
// my problem begins here, since I am using
// a static/global reference for the base url
let url = try BASE_URL.asURL()
let request = try URLRequest(url: url.appendingPathComponent(path), method: method)
guard !parameters.isEmpty else { return request }
return try JSONEncoding.default.encode(request, with: parameters)
}
}
非常感谢您的时间和帮助。
解决方案
推荐阅读
- javascript - 我有一个 excel 按钮,我想在数据表中添加 pdf 按钮
- python - 如何清除QTextEdit的文本并立即插入新文本
- excel-formula - 区分大小写的第一个出现突出显示
- xero-api - 如何获取 OAuth2 中的连接名称?
- python - 我可以读取所有 PLC 输出和输入....但我无法通过 pyads 更改 PLC 输出
- python - 我无法通过 Visual Studio 在 python 中运行我的 sigmoid 函数代码
- django - 基于客户端ID进程应该分配
- angular - 重新分组甘特图 Y 轴进行第二次休息
- node.js - Mongoose 选择随机排序的子文档(shuffle subdocuments)
- jhipster - 我已经使用 yo jhipster 创建了一个单体应用程序,但是在创建 DTO 后出现错误。我遇到以下错误