How to check code that depends on UIUserInterfaceIdiom (ios)

I have Objective-C [i-os] code that I would like to run using Xcode. It accesses various metadata depending on the type of device, using:

[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad

The problem is that when I run Unit Tests, this result is always right. Ideally, it would be great to set it as a test run.

As I suppose, this can be done by creating a class that encapsulates device validation and makes fun of it for the test. But I thought it was worth seeing if there were any better solutions.

For reference, a good blog post with many links , Apple Unit Testing Guide and Unit test sample project code .

+4
source share
1 answer

This morning I tried a couple of different approaches:

  • Using the UIDevice category, which is only related to my test purpose. In this category, I would override currentDevice with a partialMock ( OCMock ) implementation and partialMock required method so that it returns Pad or Phone forcibly to my specifications. It should have work, but it is very difficult to drop it with classes such as UIDevice or UIApplication , the simulator often crashes, which is a bad sign.

  • #undef UI_INTERFACE_IDIOM() and #define on my .pch test. UI_INTERFACE_IDIOM() specifying UI_INTERFACE_IDIOM() implementation in the test for a single instance of singleton, which I could install on a Pad or Phone, respectively. This worked, but the main problem is that when you run the tests, the simulator also goes up (tests of the applications that are), so if you run tests on the iPad, your test will pass, but other parts of the simulator will fail, because of conflicting of the answers that he receives from UI_INTERFACE_IDIOM() (one of these is loading an iPhone-specific nib if you are in a universal application environment)

  • I think this is the best approach. Like everything in computer science, just enter another layer in =) Instead of code using UI_INTERFACE_IDIOM() , to evaluate whether it is on a Pad or Phone device, encapsulate this logic into an object that you can make fun of during tests. This way, UI_INTERFACE_IDIOM() will be available to the rest of the simulator. Your production code will actually rely on it, but your tests will rely on a shaded implementation that might respond as expected in your tests.

If you want, I can share some codes on this. And yes, it is exhausted!

How did you get around this?

+4
source

Source: https://habr.com/ru/post/1391228/


All Articles