本文介绍了延迟OCMock验证/单元测试与超时处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我测试真正的Web服务调用与OCMock。
I'm testing real web service calls with OCMock.
现在我在做的的东西的,如:
- (void)testWebservice
{
id mydelegatemock = [OCMockObject mockForProtocol:@protocol(MySUTDelegate)];
[[mydelegatemock expect] someMethod:[OCMArg any]];
[SUT sutWithDelegate:mydelegatemock];
// we need to wait for real result
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]];
[(OCMockObject*)mydelegatemock verify];
}
它工作正常,但它意味着,每一个这样的测试将需要2秒。
It works fine, but it implies that every such test will take 2 seconds.
有没有一种方法我可以设置例如超时2秒,并让 mydelegatemock
到的someMethod
呼叫立即验证
并完成测试用例?
Is there a way I can set a timeout of e.g. 2 seconds, and let a call to someMethod
of mydelegatemock
immediately verify
and complete the test case?
推荐答案
我做到这一点使用一个方便实用的功能,我发现的:
I do this using a handy utility function I found at this link:
#import <Foundation/Foundation.h>
#import <OCMock/OCMock.h>
@interface TestUtils : NSObject
+ (void)waitForVerifiedMock:(OCMockObject *)mock delay:(NSTimeInterval)delay;
@end
和实施
#import "TestUtils.h"
@implementation TestUtils
+ (void)waitForVerifiedMock:(OCMockObject *)inMock delay:(NSTimeInterval)inDelay
{
NSTimeInterval i = 0;
while (i < inDelay)
{
@try
{
[inMock verify];
return;
}
@catch (NSException *e) {}
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
i+=0.5;
}
[inMock verify];
}
@end
这让我等待,最多延迟(以秒为单位),而每次等待的全部金额。
This allows me to to wait up to a maximum delay (in seconds) without waiting the full amount each time.
这篇关于延迟OCMock验证/单元测试与超时处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!