使用OCUnit测试是否显示UIAlertView [英] Using OCUnit to test if an UIAlertView is presented

查看:132
本文介绍了使用OCUnit测试是否显示UIAlertView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,只要游戏进度有效,就会在点击退出按钮时显示 UIAlertView 。我想知道如何使用OCUnit拦截 UIAlertView 并与之交互,甚至检测它是否已经呈现。我唯一能想到的是monkeypatch [UIAlertViewDelegate willPresentAlertView] ,但这让我想哭。

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?

推荐答案

更新:见我的博客文章如何对您的警报和行动表进行单元测试

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

我的另一个答案的问题是单元测试从不执行 -showAlertWithMessage:方法本身。 使用手动测试进行一次验证对于简单的场景来说并不是太糟糕,但错误处理通常涉及难以重现的异常情况。 ...此外,我有一种唠叨的感觉,我已经停止了,并且可能有更彻底的方式。有。。

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.

在被测试的类中,不要直接实例化 UIAlertView 。相反,定义一个方法

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];
}

您的测试子类(TestingFoo)重新定义 + alertViewClass 替换mock:

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

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

让您的测试类注册通知。调用的方法现在可以验证传递给警报初始化程序的参数以及消息 -show 的消息次数。

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:


  • 注册通知

  • 让我设定预期值

  • 收到通知后,根据预期值验证状态

所以我现在所做的所有警报测试都是创建验证者,设定期望并进行通话。

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

这篇关于使用OCUnit测试是否显示UIAlertView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆