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 8475215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:50:06+00:00 2026-06-10T17:50:06+00:00

My application uses third-party C APIs for some tasks. I was wondering how to

  • 0

My application uses third-party C APIs for some tasks. I was wondering how to unit test the classes that make calls to the API, since the unit testing environment is impractical to set up so that it works.

An example. One of my classes might have a method like this:

- (id) doSomeStuff
{
    // my code
    // ...
    thirdPartyDoStuffFunction(17, 4);
    // etc...
}

The corresponding unit test:

- (void) testDoSomeStuff
{
    MyClass *x = [[MyClass alloc] init];
    id result = [x doSomeStuff];

    STAssertNotNil(result, @"Result mustn't be nil");
}

Let’s say that for reasons beyond my control, calling thirdPartyDoStuffFunction while running the unit testing target will crash the test. For this reason, and because I want to make sure the function has been called with the right arguments, I would like to mock it.

This is what I have tried so far:

  • Defining preprocessor macros that redirect the third-party functions to mock versions of them.
    However, so far I couldn’t make that work. First, I have tried to define a preprocessor constant, UNIT_TEST in the unit test target’s configuration, but the constant only has effect on my unit test classes, not on the classes I am testing.
  • I have then found this thread about creating a prefix header for the test target, which seems to have exactly the same effect as the first method: The UNIT_TEST constant defined in the prefix header is only visible in the test classes, not the tested classes.

Update: Both of the above approaches work for me now. The reason they didn’t seems to be that the implementation files (.m) of the classes under test had not been added to the unit test target. Once I added them, the macros are being executed as expected, i.e. they override the third-party API.

I still wonder why there were no compiler/linker errors and the unit tests actually ran while some of the implementation wasn’t present in their target, but the important thing is that it works now.

  • 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-10T17:50:07+00:00Added an answer on June 10, 2026 at 5:50 pm

    You should create a header file TestRedirect.h with the following contents:

    #ifdef UNIT_TESTING
    
    #define thirdPartyDoStuffFunction MyTest_thirdPartyDoStuffFunction
    ..........
    
    #endif
    

    You include this file into all files with your classes that you want to test. After that you create 2 different builds:

    • one with the compiler key -DUNIT_TESTING for testing;
    • other without that key for regular work.

    Your test functions should be defined simply as MyTest_thirdPartyDoStuffFunction and they should be included into the linking only in the test configuration.

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

Sidebar

Related Questions

I have a java application that uses some third party API. The third party
I have a small application that uses the Attach API to modify some third
My application uses some third-party libraries. I need to log some exceptions that occur
I am building an android application that uses several third party libraries. I have
We have a Windows Forms application that uses a (third party) ActiveX control, and
I have an ASP.NET 4.0 web application which uses a third-party API for external
I have a Visual C++ 9 Win32 application that uses a third-party library. When
I have an MFC application that uses an ancient (circa 1999) third-party ActiveX control.
My ClickOnce application uses a third party tool that requires the Visual C++ 2005
I'm coding an application that uses a third party SDK (OCXs). I use the

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.