Skip to content

Commit c45e17f

Browse files
committed
Tests: Improve CacheTests with stub handlers
1 parent fb3c13c commit c45e17f

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

Tests/CornucopiaCoreTests/Features/CacheTests.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,15 @@ final class CacheTests: XCTestCase {
7575

7676
func testMemoryCacheOperations() {
7777
let expectation = XCTestExpectation(description: "Memory cache operation")
78+
let expectedData = testData!
79+
StubURLProtocol.handler = { request in
80+
let response = HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: nil, headerFields: nil)
81+
return (expectedData, response, nil)
82+
}
7883

7984
// Test memory cache miss
8085
cache.loadDataFor(url: testURL) { data in
81-
XCTAssertNotNil(data ?? nil, "Callback should be invoked")
86+
XCTAssertEqual(data, expectedData, "Callback should be invoked with data")
8287
expectation.fulfill()
8388
}
8489

@@ -87,11 +92,16 @@ final class CacheTests: XCTestCase {
8792

8893
func testURLRequestConversion() {
8994
let expectation = XCTestExpectation(description: "URLRequest conversion")
95+
let expectedData = testData!
96+
StubURLProtocol.handler = { request in
97+
let response = HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: nil, headerFields: nil)
98+
return (expectedData, response, nil)
99+
}
90100

91101
let urlRequest = URLRequest(url: testURL)
92102
cache.loadDataFor(urlRequest: urlRequest) { data in
93103
// Should be a consistent callback regardless of network behavior
94-
XCTAssertNotNil(data ?? nil)
104+
XCTAssertEqual(data, expectedData)
95105
expectation.fulfill()
96106
}
97107

@@ -103,12 +113,17 @@ final class CacheTests: XCTestCase {
103113
func testConcurrentCacheAccess() {
104114
let expectation = XCTestExpectation(description: "Concurrent cache access")
105115
expectation.expectedFulfillmentCount = 20
116+
let expectedData = testData!
117+
StubURLProtocol.handler = { request in
118+
let response = HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: nil, headerFields: nil)
119+
return (expectedData, response, nil)
120+
}
106121

107122
// Simulate concurrent access to the same URL
108123
for _ in 0..<20 {
109124
DispatchQueue.global().async {
110125
self.cache.loadDataFor(url: self.testURL) { data in
111-
XCTAssertNotNil(data ?? nil) // ensure callback executes
126+
XCTAssertEqual(data, expectedData) // ensure callback executes
112127
expectation.fulfill()
113128
}
114129
}

0 commit comments

Comments
 (0)