Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any chance to write unit tests against EKEventStore?

We are implementing "Sync to Calendar" functionality within our application. Our synchronization process involves events that we obtain from server-side that we want to replicate to iPhone calendar. Currently I am not sure if the method that we wrote for this synchronization addresses all possible scenarios we expect to have this is why I want to unit test it. It contains numerous calls to EKEventStore which, as Apple documentation states, needs to be called with requestAccessToEntityType:completion: first:

On iOS 6 and later, you must request access to an entity type after the event store is initialized with requestAccessToEntityType:completion: for data to return.

...which will be difficult to handle within environment in which our unit tests are run.

Currently I am thinking about implementing mock subclass of EKEventStore with some NSArray of EKEvent objects behind it.

Is there are any possibility to unit test against EKEventStore without writing such mock subclass which would mimic all the methods we use from inside our synchronization routine?

like image 468
Stanislav Pankevich Avatar asked Aug 20 '14 16:08

Stanislav Pankevich


1 Answers

Recently I discovered that it is finally possible to do that in Simulator using special hack: there is TCC.db database where this and some other access permissions are stored.

I was able to set up my unit test suite so that when it starts this TCC.db table is modified with value that corresponds to EKAuthorizationStatusAuthorized. After this change is done all subsequent tests can be written with full access to EKEventStore.

First of all a bit of explanation is here: Grant access to NAB programatically on iOS 7.0 Simulator.

Also there is nice project JPSimulatorHacks which contains code wrapping up this hack. It does support granting permissions to Calendar.

Now I am able to unit-test my Calendar Sync code without exhaustive stubbing of EK-classes.

Important detail:

There is no access to TCC.db at least I didn't find it when your test target does not point to real application in Host Application. When I did set Host Application to None I was not able locate TCC.db and hence grant anything. That's why currently for my unit tests target I created artificial application MyAppNameTestsApp so now I set my unit test target's Host Application to be MyAppNameTestsApp. This gives me valid app with TCC.db and at the same time does not require me to load the whole application when I run Cmd+U.

like image 113
Stanislav Pankevich Avatar answered Nov 10 '22 06:11

Stanislav Pankevich