-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Milestone
Description
Replace integration tests with mocked URLSession tests.
These integration tests have several reliability and maintainability concerns:
- External dependencies: Tests rely on external services (apple.com, httpbin.org) which can fail, timeout, or change behavior
- Flakiness: Network issues, service outages, or rate limiting will cause non-deterministic test failures
- CI/CD impact: Tests will fail in offline, sandboxed, or network-restricted environments
- Performance: Network calls significantly slow down test execution
- Weak assertions (lines 50, 71): Using
_ = lastModifieddoesn't validate behavior
As per coding guidelines: "Use mock objects from BushelTestUtilities for testing protocol conformances and creating test doubles."
Recommended approach: Mock URLSession using protocol-based design
Create a protocol for URLSession operations in BushelUtilities:
public protocol URLSessionProtocol {
func data(from url: URL) async throws -> (Data, URLResponse)
func data(for request: URLRequest) async throws -> (Data, URLResponse)
}
extension URLSession: URLSessionProtocol {}Then add mocks in BushelTestUtilities and test with controlled responses.
Would you like me to help implement this mocking infrastructure?
🤖 Prompt for AI Agents
In @Tests/BushelUtlitiesTests/URLSessionTests.swift around lines 38-107, The
tests in URLSessionTests (methods testFetchLastModifiedWithValidURL,
testFetchLastModifiedWithInvalidURL, testFetchLastModifiedReturnsNilGracefully,
testFetchDataWithValidURL, testFetchDataWithoutLastModifiedTracking,
testFetchDataWithInvalidURLThrows) are integration tests hitting real networks;
replace them with deterministic unit tests using a URLSessionProtocol and mocks
from BushelTestUtilities. Add a URLSessionProtocol (methods data(from:) and
data(for:)), extend URLSession to conform, create a MockURLSession in
BushelTestUtilities that returns controlled (Data, URLResponse) or throws,
update the code under test to accept a URLSessionProtocol (or provide an
injectable session on URLSession.shared wrappers like fetchLastModified and
fetchData), rewrite each test to inject the mock responses (including a
404/timeout/error case) and replace `_ = lastModified` with concrete assertions
(e.g., XCTAssertEqual/ XCTAssertNil against the mocked Last-Modified header or
data content).
Originally posted by @coderabbitai[bot] in #126 (comment)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels