gpt4 book ai didi

swift - Swift 中的单元测试 URL 参数

转载 作者:行者123 更新时间:2023-11-30 12:46:46 26 4
gpt4 key购买 nike

我正在尝试将 URL 组合起来在 Swift 中使用 TDD。我想确保URL等于预期的URL 。我正在使用 Router enum ,使用Alamofire ,创建我的URL s。因此,作为 Router 的属性enum ,我有一个Dictionary<String,String>?属性,因为我可能不需要其他 URL 的参数s。但是,我确实需要身份验证参数 URL .

parameters Dictionary<String,String>?如下:

internal enum Router: URLConvertible {

internal static let baseURLString = "https://xxx.xxxxxx.xxx"

case authentication(clientID: String)

}

extension Router {

var pathString: String {
switch self {
case .authentication: return "xxxxx/xxxxx/xxxxxxxxx"
}
}

var method: HTTPMethod {
switch self {
case .authentication: return .get
}
}

var parameters: Dictionary<String, String>? {
switch self {
case .authentication(let clientID): return ["xxxxx": "xxxx", "client_id": clientID]
}
}

func asURL() throws -> URL {
switch self {
case .authentication:
return URL(string: "https://xxx.xxxxxx.xxx/xxxxx/xxxxx/xxxxxxxxx?xxxxx=xxxx&client_id=xxxxxxxxxx")!
}
}

}

到目前为止,我已经能够成功地以 TDD 方式编写所有内容。我陷入困境的是测试返回的 URL 。自 parameters类型为Dictionary<String,String>? ,我想迭代parameters通过使用以下逻辑:

var parametersStringArray = Array<Dictionary<String,String>>()

for (key, value) in parameters {
// Create a parameter string from each set
// Something like the following:
let parameterString = String(describing: \(key)=\(value))
parametersStringArray.append(parameterString)
}

let parametersString = parametersStringArray.joined(separator: "&")
let fullURLString = baseURLString + "?\(parametersString)"
return URL(string: fullURLString)!

通过创建这样的参数,当我测试预期的 URL 时针对返回URL来自Router enumasURL() throws -> URL ,由于参数被添加到 URL 中,它可能会在部分时间失败。没有预期顺序的字符串。换句话说,如果我预期的 URL 是 https://xxx.xxxxxx.xxx?xxxxx=xxxx&client_id=xxxxxxxxxx ,它可以通过按相反顺序返回参数来匹配或失败,如下所示:https://xxx.xxxxxx.xxx?client_id=xxxxxxxxxx&xxxxx=xxxx .

有没有更好的解决方案让我测试 URL确保正确URL当参数可以按任何顺序时返回,因为它们被添加到 URL迭代它们时的字符串?

我应该创建两个 URL s 具有两种方式的参数并与任一方式匹配?我认为不合适的原因是参数越多, URL 就越多。您必须创建来进行测试。

我当前的测试用于测试 URL对于身份验证,请遵循:

import XCTest
import Alamofire
@testable import Projects

class AuthenticationURLTests: XCTestCase {

func test_API_BaseURLString_IsCorrect() {
let baseURLString = Router.baseURLString
let expectedBaseURLString = "https://xxx.xxxxxx.xxx/"
XCTAssertEqual(baseURLString, expectedBaseURLString, "Base URL does not match expected base URL. Expected base URLs to match.")
}

func test_API_OAuthPath_IsCorrect() {
let pathString = Router.authentication(clientID: "xxxxxxxxxx").pathString
let expectedPathString = "xxxxx/xxxxx/xxxxxxxxx"
XCTAssertEqual(pathString, expectedPathString, "Path string does not match expected path string. Expected path strings to match.")
}

func test_API_OAuthMethod_IsCorrect() {
let method = Router.authentication(clientID: "xxxxxxxxxx").method
let expectedMethod = HTTPMethod.get
XCTAssertEqual(method, expectedMethod, "HTTP methods do not match. Expected HTTP methods to match.")
}

func test_API_OAuth_ParametersContainsKeyForXXXXX() {
let scopeString = Router.authentication(clientID: "xxxxxxxxxx").parameters?["xxxxx"]
let expectedScopeString = "xxxx"
XCTAssertEqual(scopeString, expectedScopeString, "Scope strings do not match. Expected scope strings to match.")
}

func test_API_OAuth_ParametersContainsKeyForClientID() {
let clientID = Router.authentication(clientID: "12345abcde").parameters?["client_id"]
let expectedClientID_Fake = "12345abcde"
XCTAssertEqual(clientID, expectedClientID_Fake, "Client IDs do not match. Expected client IDs to match.")
}

func test_API_OAuth_URLIsCorrect() {
do {
let url = try Router.authentication(clientID: "12345abcde").asURL()
// Here, I want to ensure that the `URL`s match:
let expectedURL = URL(string: https://xxx.xxxxxx.xxx?xxxxx=xxxx&client_id=xxxxxxxxxx)!
} catch { XCTFail("Could not create URL for OAuth") }
}

}

对所有 x 表示抱歉s 在提供的代码中。我想确保我没有提供有关客户申请的任何信息。

最佳答案

您可以使用URLComponents .

这个类对于操作 URL 非常方便。例如,它可以是 initialized using a URL instance ,然后通过 queryItems property 列出数组中的所有查询参数.

Where I am stuck is testing the returned URL.

URLComponents 也符合 Equatable,因此您可以测试返回的 URL 值是否符合您的预期,如下所示:

  1. 创建一个 URLComponents 实例,并配置您期望的所有参数。这是测试的安排部分。
  2. 通过 asURL() 方法获取 URL 的值。这是测试的act部分。
  3. 使用刚刚获得的 URL 值创建一个新的 URLComponents 实例,然后将其与使用预期值构建的 URLComponents 进行比较,例如通过 XCTAssertEqual 。这是测试的断言部分。

一旦所有测试都通过,您可能需要查看 Router 的实现,看看是否也可以在其中使用 URLComponents。该类由 Apple 提供,非常健壮。

关于swift - Swift 中的单元测试 URL 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41539039/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com