4 Replies Latest reply: Oct 6, 2012 5:18 PM by Guven Bolukbasi RSS

iOS - Unit testing an application that uses SUP?

Aryan Ghassemi
Currently Being Moderated

We are using SUP on our project and everything seems to be working well.

We decided to create a unit test target and write some unit tests, but SUP crashes as soon as the tests start (only on 2.1.3).

Anyone else is having trouble running unit tests in an SUP based app?

Is there a simple way to mock SUP?

 

Screen Shot 2012-09-06 at 2.32.29 PM.png

  • Re: iOS - Unit testing an application that uses SUP?
    Aryan Ghassemi
    Currently Being Moderated

    All my SUP logic is in a singleton class, so I ended up overriding the shared instance method using a category and prevent the crash using some reflection API, In the rest of the app wherever SUP is needed I create a mock (using OCMock), and assign the expected behavior.

     

    + (id)sharedInstance

    {

              Method originalMethod = class_getClassMethod([MessagingClientLib class], @selector(getInstance));

        Method categoryMethod = class_getClassMethod([self class], @selector(mockGetInstance));

        method_exchangeImplementations(originalMethod, categoryMethod);

     

              originalMethod = class_getClassMethod(NSClassFromString(@"SUPOCHandler"), @selector(getInstance));

        categoryMethod = class_getClassMethod(NSClassFromString(@"SUPOCHandler"), @selector(mockInitialize));

        method_exchangeImplementations(originalMethod, categoryMethod);

    }

     

    + (id)mockGetInstance

    {

       return [OCMockObject niceMockForClass:[MySupManager class]];

    }

     

    + (void)mockInitialize

    {

       // Do nothing

    }

  • Re: iOS - Unit testing an application that uses SUP?
    Guven Bolukbasi
    Currently Being Moderated

    Hello Aryan,

     

    That is a really good workaround to make sure that you can actually execute tests. It should be a lot easier than this to test our SUP applications but that is all we have for now.

     

    Just one question: Did you have time to dig deeper on why this crash is happening?

     

    - My first guess was that I wasn't initializing the SUP connection when the unit tests launch. However, I have checked to see that when my main application starts, there is absolutely no SUP logic. In other words, I am not accessing any SUP classes until the user presses a button. As a result, there shouldn't be any difference between when my application launches and when my unit test target (application unit tests) launches. However, the crash only occurs during the unit test launch.

     

    Let me know if you were able to find why the crash only occurs during unit tests.

     

    Also, you would probably know this but you can use 'logic unit tests' as an alternative. Simply create a unit test target of type 'logic tests' and don't include any SUP relevant classes in its Build Phases -> Compile Sources. That way, you can test your model/business logic in isolation.

     

    Cheers,

    Guven.

Actions

Incoming Links