How to mock EKEventStore

Viewed 607

I have a simple function for requesting access for adding events in the Calendar.

typealias BoolCallback = (Bool) -> ()

class SimpleCalendar {

    let store: EKEventStore

    init(store: EKEventStore = EKEventStore()) {
        self.store = store
    }

    /// Request access to EventKit API for Calendar intergration
    ///
    /// - Parameter callback: returns if access was recived or denied
    func requestAccess(callback: @escaping BoolCallback) {

        let status = EKEventStore.authorizationStatus(for: .event)

        switch status {
        case .authorized:
            callback(true)
            //        case .notDetermined: //should ask for access
        //        case .denied, .restricted: //should open a ask permission view
        default:
            store.requestAccess(to: .event) { accessGranted, _ in

                DispatchQueue.main.async {
                    callback(accessGranted)
                }
            }
        }


    }
}

Is there a way to mock EKEventStore for getting the status?

I also have a simple function that checks if a calendar exists.

func doesCalendarExist(name: String) -> Bool {
    let calendars = store.calendars(for: .event)

    if calendars.contains(where: { $0.title == name }) {
        return true
    }

    return false
}

Is there a way to mock the store objects for getting store.calendars

Thanks


I switched

let status = EKEventStore.authorizationStatus(for: .event)

to

let status = type(of: store).authorizationStatus(for: .event)
1 Answers
Related