/ BalanceKitTests / MockHealthKitService.swift
MockHealthKitService.swift
1 // 2 // MockHealthKitService.swift 3 // BalanceKitTests 4 // 5 // Created by Alexander Kunau on 13.12.25. 6 // 7 8 import Foundation 9 import HealthKit 10 @testable import BalanceKit 11 12 class MockHealthManager: HealthManager { 13 var mockIsAuthorized: Bool = true 14 var mockActiveCaloriesBurned: Double = 0 15 var mockSteps: Int = 0 16 var mockWaterConsumed: Double = 0 17 var shouldFailNextOperation: Bool = false 18 var authorizationCallCount: Int = 0 19 var saveCallCount: Int = 0 20 21 override var isAuthorized: Bool { 22 get { mockIsAuthorized } 23 set { mockIsAuthorized = newValue } 24 } 25 26 override var activeCaloriesBurned: Double { 27 get { mockActiveCaloriesBurned } 28 set { mockActiveCaloriesBurned = newValue } 29 } 30 31 override var steps: Int { 32 get { mockSteps } 33 set { mockSteps = newValue } 34 } 35 36 override var waterConsumed: Double { 37 get { mockWaterConsumed } 38 set { mockWaterConsumed = newValue } 39 } 40 41 override init() { 42 super.init() 43 self.mockIsAuthorized = true 44 } 45 46 override func requestAuthorization() { 47 authorizationCallCount += 1 48 if shouldFailNextOperation { 49 mockIsAuthorized = false 50 } else { 51 mockIsAuthorized = true 52 } 53 } 54 55 override func saveFoodItem(name: String, calories: Double, protein: Double, carbs: Double, fat: Double) { 56 saveCallCount += 1 57 if shouldFailNextOperation { 58 // Simulate failure - do nothing 59 return 60 } 61 // Simulate success 62 } 63 64 override func addWater(milliliters: Double) { 65 if shouldFailNextOperation { 66 // Simulate failure 67 return 68 } 69 mockWaterConsumed += milliliters 70 objectWillChange.send() 71 } 72 73 override func fetchTodaysData() { 74 if !shouldFailNextOperation { 75 mockActiveCaloriesBurned = 250.0 76 mockSteps = 5000 77 mockWaterConsumed = 1000.0 78 } 79 } 80 81 override func fetchActiveCaloriesForDate(_ date: Date, completion: @escaping (Double) -> Void) { 82 if shouldFailNextOperation { 83 completion(0) 84 } else { 85 completion(mockActiveCaloriesBurned) 86 } 87 } 88 89 override func fetchStepsForDate(_ date: Date, completion: @escaping (Int) -> Void) { 90 if shouldFailNextOperation { 91 completion(0) 92 } else { 93 completion(mockSteps) 94 } 95 } 96 97 override func fetchWaterIntakeForDate(_ date: Date, completion: @escaping (Double) -> Void) { 98 if shouldFailNextOperation { 99 completion(0) 100 } else { 101 completion(mockWaterConsumed) 102 } 103 } 104 }