[Technical Questions] Testing inaccessible errors #173
Replies: 1 comment 5 replies
-
Hi @nick-allen21! The There is no workaround for this other than changing the access level of the properties of the view, which you might not want to do! Alternatively, you could test this with a UITest instead injecting mock Bluetooth devices. Finally, you could package a lot of the code that manages State into an Observable ViewModel. This is a common design pattern in Swift/SwiftUI that would allow you to expose a lot of functions and state to Internal access level and Unit Testing. However, this would likely require a decent amount of code restructuring. Let me know if you have any questions about any of these approaches! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
In what area do you have a technical challenge?
Xcode, Simulator & Previews
Description
I am getting a ton of errors saying that I cannot access the private function that I am trying to test on. I have spent sometime debugging and looking up documentation but to no avail.

Reproduction
My TestingTest branch should be public on Quanbio industry repo. You can go to that and try to run the test cases. Here is the code if that would help
`import XCTest
import SwiftUI
@testable import QuanBioSync
// MARK: - Expose the parseAndAppend function for testing.
// You can add this in a separate file in your test target.
@mainactor
extension TestView {
// Changing the access level (or using @testable) allows tests to call this function.
func test_parseAndAppend(_ data: Data) {
self.parseAndAppend(data)
}
}
@mainactor
final class TestViewTests: XCTestCase {
// // Test the Battery Level package (cmdID = 0x99).
// func testBatteryLevel() {
// // Build a valid Battery Level package:
// // Header: [0xff, 0xfe]
// // For this package, we use a different length byte (here 0x0c) and a dummy checksum.
// // Then: DeviceID: 0x23, cmdID: 0x99, followed by one byte for the battery level.
// let bytes: [UInt8] = [0xff, 0xfe, 0x0c, 0xbc, 0x23, 0x99, 80] // battery level = 80
// let data = Data(bytes)
// testView.test_parseAndAppend(data)
// waitForAsyncUpdates()
//
// XCTAssertEqual(testView.dynamicData.batteryStatus.count, 1)
// XCTAssertEqual(testView.integerDynamicData.batteryStatus.count, 1)
// }
}
`
`@MainActor
struct TestView: View {
@Environment(MyDevice.self) var myDevice: MyDevice?
@State private var dynamicData = LocalDynamicRawDataRequest()
@State private var integerDynamicData = IntegerDynamicRawDataRequest()
@State private var deviceStateDescription: String = ""
@State private var isNavigatingToPersonView = false
@State private var pulseRate: UInt8?
@State private var SPO2: UInt8?
@State var testRiddle: Riddle = getRandomRiddle()
@State var properDataRead: Bool = false
@State var notifyBool: Bool = false
let totalTime: Double = 90
/// Rest of code
`
Expected behavior
I expected the tests to run
Additional context
No response
Code of Conduct
Beta Was this translation helpful? Give feedback.
All reactions