I'm developing a calendar app where I'm struggling to create tests for the functions that utilize the calendars on the users device. The
calendar: EKCalendar
variable is taken from the users event store so when the unit tests run on XCode's emulator they fail as the calendar doesn't exist. Using my personal device works but then the tests would fail on our build server.
What are good approaches to test a function/computed property that uses the User's calendar/eventstore?
///Return the user's name. If this hasn't been saved extract their name from their Enterprise calendar and save it.
static var name: String? {
if let savedName = defaults.string(forKey: "name") {
return savedName
}
// calendar is a specific EKCalendar members of my company will have.
guard let calendar = MeetingsFetcher().getUserEnterpriseCalendars().first,
let parsedName = calendar.title.firstMatch(from: Regex.organizerNameFromEKCalendar) else {
return nil
}
defaults.set(parsedName, forKey: "name")
return parsedName
}
func getEnterpriseCalendars() -> [EKCalendar]{
guard EKEventStore.authorizationStatus(for: .event) == .authorized else { return [EKCalendar]() }
for calendar in MeetingsFetcher.eventStoreClass.calendars(for: .event) {
if calendar.source.title.range(of: "IBM") != nil{
return [calendar]
}
}
return [EKCalendar]()
}
Ignore that it is returning an array, I'm not really sure why it does that :p