gpt4 book ai didi

iphone - OCUnit 可以依赖后台线程中的代码吗? (为了测试异步请求)

转载 作者:行者123 更新时间:2023-12-03 13:22:03 24 4
gpt4 key购买 nike

我一直在尝试对异步请求进行自动化测试,但是在测试功能等待时,我无法在不同的线程中运行任何东西。
这是测试功能:

- (void) testBoxManagerConnexionStatus
{
ControlSender* cs = [ControlSender get];
requestShouldSucceed = YES;
[cs startCheckingReachabilityWithDelegate:self];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:TIMEOUT_INTERVAL+1.0]];

STAssertTrue(downloadComplete, @"Download should be over by now");
}

我的测试类以这种方式实现回调方法:
- (void)controlSender:(ControlSender *)controlSender sentSuccessfullyCode:(FreeboxControl)code
{
if (requestShouldFail) {
STAssertTrue(NO, @"Request should have failed");
}
downloadComplete = YES;

}
- (void)controlSender:(ControlSender *)controlSender couldntSendCode:(FreeboxControl)code details:(NSHTTPURLResponse*)details
{
if (requestShouldSucceed) {
STAssertTrue(NO, @"Request should have succeded");
}
downloadComplete = YES;
}

但是,每当我常用的代码尝试在不同的线程中运行某些东西时,什么都不会发生。例如 NSURLConnection 在分配时从不调用它的委托(delegate)方法:
m_connexion = [[NSURLConnection alloc] initWithRequest:m_networkRequest delegate:self];
-connectionDidFinishLoading:也不是 -connection:didFailWithError:
像这样的电话也是如此:
[self performSelectorInBackground:@selector(BG_startCheckingReachabilityWithDelegate:) withObject:delegate];

运行测试时,Nothings 在后台被调用。
不过,相同的代码在测试之外也能正常工作。
有没有办法用 OCUnit 测试异步 url 请求?

谢谢您的帮助。

最佳答案

你可以看看https://github.com/danielpunkass/RSTestingKit有一种方法可以在单元测试中等待运行循环,您可以在 http://www.red-sweater.com/talks/UnitTesting.pdf 看到他的幻灯片对于一些背景。它可能有一些信息可以帮助您入门。

关于iphone - OCUnit 可以依赖后台线程中的代码吗? (为了测试异步请求),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4307011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com