Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8157759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:23:39+00:00 2026-06-06T17:23:39+00:00

I have some Objective-C [i-os] code that I would like to run unit tests

  • 0

I have some Objective-C [i-os] code that I would like to run unit tests over using XCode. It accesses different metadata depending on the type of device, using:

[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad

The problem I have is that when I run the Unit Tests, this result is always true. Ideally, it would be great to be able to set it as the test runs.

The way I guess it could be done is by creating a class that encapsulates the device check and mock that for the test. But I thought it might be worth seeing if there are any better solutions.

For reference, a nice blog post with many links, the Apple Unit testing guide and Unit test sample code project.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:23:40+00:00Added an answer on June 6, 2026 at 5:23 pm

    I tried a couple different approaches this morning:

    1. Using a UIDevice category that is only linked to my test target. In that category I would override currentDevice with a partialMock implementation (OCMock) and stub the necessary method so that it returns Pad or Phone forcedly to my specifics tests. It should have work, but its very difficult to mess with classes such as UIDevice or UIApplication, the simulator often crashes which is a bad sign.

    2. #undef UI_INTERFACE_IDIOM() and #define it on my test .pch. Pointing UI_INTERFACE_IDIOM() implementation in the test to a singleton instance of my own, that I could set to Pad or Phone accordingly. That worked, but the main problem is that when you run the tests the simulator also goes up ( application tests that is ) so if you are running your tests on the iPad, your test will pass, but other parts of the simulator will fail because of the contraditory responses it gets from UI_INTERFACE_IDIOM() (one of such is the loading of nib’s specific to iPhone if you are in a universal app environment )

    3. I think this is the best approach. As everything in computer science, just put another layer in =) Instead of your code using UI_INTERFACE_IDIOM() to assess if it is on a Pad or Phone device, encapsulate this logic in a object that you can mock during your tests. That way UI_INTERFACE_IDIOM() will still be available to the rest of the simulator. Your production code will actually rely on it, but your tests will rely on a stubbed implementation that can respond as expected in your tests.

    If you want to, I can share some codes regarding this matter. And yes, this is a weary one!

    How you managed to workaround this?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Objective-C code that looks like this: #define myVar 10 float f
I have some C code in an iOS project that I would like to
I have some Code that I'd like to share between an iOS and an
I have a compiled external library that I'm using in my (Objective-C++) code. This
I have some files that are uuencoded, and I need to decode them, using
I have some jquery code that is doing an ajax lookup and returning comma
I have an NSTextField that I would like to enable as-you-type spell checking. When
I would like to know some libraries in objective-C for xml parsing. I think
I have some Objective-C code to transition between two views. I have the transition
I'm writing some library code, and users of the library sometimes have different opinions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.