Skip to content

Commit

Permalink
Add missing valueNotFound unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dogo committed Aug 23, 2024
1 parent 1844205 commit 9932eda
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions SWDestinyTradesTests/Rest/HttpClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,27 @@ final class HttpClientTests: XCTestCase {
}

func test_request_with_failure_valueNotFound() async throws {
struct DummyResponse: Decodable {
let id: Int
let name: String
}

let missingValueJson = Data("{ \"id\": 123, \"name\": null }".utf8)

URLProtocolMock.response = { _ in
HTTPResponse(data: Data("{ \"bar\": \"invalid_value\" }".utf8),
statusCode: 200)
return HTTPResponse(data: missingValueJson, statusCode: 200)
}

do {
_ = try await sut.request(request, decode: Foo.self)
XCTFail("Expected to throw while awaiting, but succeeded")
_ = try await sut.request(request, decode: DummyResponse.self)
XCTFail("Expected to throw APIError.valueNotFound, but succeeded.")
} catch {
// XCTAssertEqual(error as? APIError, .valueNotFound(type: Foo.self, context: "Value for 'Foo' could not be decoded."))
if let apiError = error as? APIError, case let .valueNotFound(type, context) = apiError {
XCTAssertEqual(String(describing: type), String(describing: String.self), "Expected type String for 'name'.")
XCTAssertTrue(context.contains("Cannot get unkeyed decoding container -- found null value instead"), "Unexpected context message: \(context)")
} else {
XCTFail("Expected APIError.valueNotFound, but got \(error) instead.")
}
}
}

Expand Down

0 comments on commit 9932eda

Please sign in to comment.