6

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?

4

1 回答 1

7

最近我发现终于可以在模拟器中使用特殊的技巧来做到这一点:有一个TCC.db数据库存储了这个和其他一些访问权限。

我能够设置我的单元测试套件,以便在它启动时TCC.db使用对应于EKAuthorizationStatusAuthorized. 完成此更改后,可以编写所有后续测试,并具有对EKEventStore.

首先这里有一点解释:Grant access to NAB programatically on iOS 7.0 Simulator

还有一个不错的项目JPSimulatorHacks,它包含了这个 hack 的代码。它确实支持授予日历权限。

现在我可以对我的日历同步代码进行单元测试,而无需对 EK 类进行详尽的存根。

重要细节:

当您的测试目标未TCC.db指向Host Application. 当我设置为时Host ApplicationNone我无法找到TCC.db并因此授予任何东西。这就是为什么目前我为我的单元测试目标创建了人工应用程序 MyAppNameTestsApp 所以现在我将我的单元测试目标设置Host ApplicationMyAppNameTestsApp. 这为我提供了有效TCC.db的应用程序,同时不需要我在运行时加载整个应用程序Cmd+U

于 2015-01-25T11:44:49.620 回答