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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:19:12+00:00 2026-05-22T18:19:12+00:00

I’m working on an app that will display a UIAlertView upon hitting it’s exit

  • 0

I’m working on an app that will display a UIAlertView upon hitting it’s exit button, only if progress in the game has been made. I was wondering how you would use OCUnit to intercept the UIAlertView and interact with it, or even detect if it has been presented. The only thing I can think of is to monkeypatch [UIAlertViewDelegate willPresentAlertView], but that makes me want to cry.

Does anyone know of a better method of doing this?

  • 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-05-22T18:19:13+00:00Added an answer on May 22, 2026 at 6:19 pm

    Update: See my blog post How to Unit Test Your Alerts and Action Sheets

    The problem with my other answer is that the -showAlertWithMessage: method itself is never exercised by unit tests. “Use manual testing to verify it once” isn’t too bad for easy scenarios, but error handling often involves unusual situations that are difficult to reproduce. …Besides, I got that nagging feeling that I had stopped short, and that there might be a more thorough way. There is.

    In the class under test, don’t instantiate UIAlertView directly. Instead, define a method

    + (Class)alertViewClass
    {
        return [UIAlertView class];
    }
    

    that can be replaced using “subclass and override.” (Alternatively, use dependency injection and pass this class in as an initializer argument.)

    Invoke this to determine the class to instantiate to show an alert:

    Class alertViewClass = [[self class] alertViewClass];
    id alert = [[alertViewClass alloc] initWithTitle:...etc...
    

    Now define a mock alert view class. Its job is to remember its initializer arguments, and post a notification, passing itself as the object:

    - (void)show
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:MockAlertViewShowNotification
                                                            object:self
                                                          userInfo:nil];
    }
    

    Your testing subclass (TestingFoo) redefines +alertViewClass to substitute the mock:

    + (Class)alertViewClass
    {
        return [MockAlertView class];
    }
    

    Make your test class register for the notification. The invoked method can now verify the arguments passed to the alert initializer and the number of times -show was messaged.

    Additional tip: In addition to the mock alert, I defined an alert verifier class that:

    • Registers for the notification
    • Lets me set expected values
    • Upon notification, verifies the state against the expected values

    So all my alert tests do now is create the verifier, set the expectations, and exercise the call.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want use html5's new tag to play a wav file (currently only supported

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.