Objective-C中协议/回调/委托的OCUnit测试 [英] OCUnit test for protocols/callbacks/delegate in Objective-C

查看:173
本文介绍了Objective-C中协议/回调/委托的OCUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用OCUnit可以测试代理协议吗?

Using OCUnit, is there a way to test delegate protocols?


-(void) testSomeObjDelegate {
  SomeObj obj = [[SomeObj alloc] initWithDelegate:self];
  [obj executeMethod];
}
-(void) someObjDelegateMethod {
  //test something here
}

我将尝试在不同的线程上调用 obj 方法,并在测试代码被调用之前进行测试休眠。似乎应该有一个更简单的方法来测试这个。

I'm going to try calling the obj method on a different thread and have the test sleep until the delegate is called. It just seems like there should be an easier way to test this.

推荐答案

测试代表是微不足道的。只需在你的回调方法中设置一个ivar,然后检查它应该触发代理回调。

Testing a delegate is trivial. Just set an ivar in the test in your callback method, and check it after what should be triggering the delegate callback.

例如,如果我有一个类某些使用协议代理 SomethingDelegate 并发送该委托 -something:delegateInvoked:为了回应一些消息,我可以测试它的比例ethis:

For example, if I have a class Something that uses a delegate of protocol SomethingDelegate and sends that delegate -something:delegateInvoked: in response to some message, I can test it lik ethis:

@interface TestSomeBehavior : SenTestCase <SomethingDelegate>
{
    Something *_object;
    BOOL _callbackInvoked;
}
@end

@implementation TestSomeBehavior

- (void)setUp {
    [super setUp];
    _object = [[Something alloc] init];
    _object.delegate = self;
}

- (void)tearDown {
    _object.delegate = nil;
    [_object release];
    [super tearDown];
}

- (void)testSomeBehaviorCallingBack {
    [_object doSomethingThatShouldCallBack];

    STAssertTrue(_callbackInvoked,
                 @"Delegate should send -something:delegateInvoked:");
}

- (void)something:(Something *)something delegateInvoked:(BOOL)invoked {
    _callbackInvoked = YES;
}

@end

我想你已经明白了但是,从你的方式说出你的问题。 (我大部分是为其他读者发帖。)我想你其实是在问一个更微妙的问题:如何测试某些可能会在以后出现的东西,比如旋转跑步的东西。我的提示是你提到的睡眠和线程。

I think you already understand this, however, from the way you've phrased your question. (I'm mostly posting this for other readers.) I think you're actually asking a more subtle question: How do I test something that may occur later such as something that spins the runloop. My cue is your mention of sleeping and threading.

首先,你不应该只是在另一个线程上任意调用一个方法。您应该只有这样做,如果它被记录为以这种方式安全使用。原因是你不知道班级内部的内容。例如,它可能会在运行循环中调度事件,在这种情况下,在不同的线程上运行该方法将使它们在不同的运行循环中发生。这将会使课堂的内部状态变得困难。

First off, you should not just arbitrarily invoke a method on another thread. You should only do so if it's documented to be safe to use in that way. The reason is that you don't know what the internals of the class do. For example, it might schedule events on the run loop, in which case running the method on a different thread will make them happen on a different run loop. This would then screw up the class's internal state.

如果您 需要测试可能需要一点时间才能发生的事情,您可以只需运行当前的运行循环即可。以下是我可以重写上述单个测试方法:

If you do need to test something that may take a little time to happen, you can do this just by running the current run loop. Here's how I might rewrite the individual test method above to do that:

- (void)testSomeBehaviorCallingBack {
    NSDate *fiveSecondsFromNow = [NSDate dateWithTimeIntervalSinceNow:5.0];

    [_object doSomethingThatShouldCallBack];

    [[NSRunLoop currentRunLoop] runUntilDate:fiveSecondsFromNow];

    STAssertTrue(_callbackInvoked,
                 @"Delegate should send -something:delegateInvoked:");
}

这将使默认模式下的当前运行循环旋转5秒,假设 -doSomethingThatShouldCallBack 将在默认模式下的主运行环路上调度其工作。这通常是正常的,因为以这种方式工作的API通常允许您指定运行循环以及运行模式。如果可以这样做,则可以使用 - [NSRunLoop runMode:beforeDate :] 以这种方式运行运行循环,从而使您希望完成的工作更有可能是。

This will spin the current run loop in the default mode for 5 seconds, under the assumption that -doSomethingThatShouldCallBack will schedule its work on the main run loop in the default mode. This is usually OK because APIs that work this way often let you specify a run loop to use as well as a mode to run in. If you can do that, then you can use -[NSRunLoop runMode:beforeDate:] to run the run loop in just that mode instead, making it more likely that the work you're expecting to be done will be.

这篇关于Objective-C中协议/回调/委托的OCUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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