在为 Objective C 进行单元测试时,如何测试块?

发布于 2024-11-01 02:33:29 字数 413 浏览 0 评论 0原文

我有一个函数(内部使用 ASIHTTPRequest),它调用一个带有结果的块:

[Http get:@"http://api.geonames.org/postalCodeLookupJSON"
   params:params cacheMins:0 complete:^(NSDictionary *response, BOOL success) {
       STAssertTrue(success, @"JSON retrieved OK");
       STFail(@"blah");
}];

我想测试上面的内容,但似乎测试没有被调用。

我如何确保测试等到块被调用?

-编辑-

当然,我不建议在 gui 线程的主应用程序中执行此操作,在这种特殊情况下,它仅用于单元测试。

I have a function (internally uses ASIHTTPRequest) which calls a block with the result:

[Http get:@"http://api.geonames.org/postalCodeLookupJSON"
   params:params cacheMins:0 complete:^(NSDictionary *response, BOOL success) {
       STAssertTrue(success, @"JSON retrieved OK");
       STFail(@"blah");
}];

I want to test the above, but it seems the test doesn't get called.

How can i ensure that the test waits till the block is called?

-edit-

Of course i don't recommend to do this in the main app in the gui thread, in this particular situation it is only for a unit test.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮坠入山谷 2024-11-08 02:33:29

找到了解决方案:

等待代码完成执行

例如:

__block int done=0;
[Http get:@"http://api.geonames.org/postalCodeLookupJSON"
   params:params cacheMins:0 complete:^(NSDictionary *response, BOOL success) {
       STAssertTrue(success, @"JSON retrieved OK");
       NSArray *postalcodes = [response objectForKey:@"postalcodes"];
       NSDictionary *first = [postalcodes objectAtIndex:0];
       NSString *adminName1 = [first objectForKey:@"adminName1"];
       STAssertTrue([adminName1 isEqualToString:@"New South Wales"], @"NSW");
       done=1;
}];

// https://stackoverflow.com/questions/3615939/wait-for-code-to-finish-execution
while (!done) {
    // This executes another run loop.
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    // Sleep 1/100th sec
    usleep(10000);
}

Found a solution:

Wait for code to finish execution

Eg:

__block int done=0;
[Http get:@"http://api.geonames.org/postalCodeLookupJSON"
   params:params cacheMins:0 complete:^(NSDictionary *response, BOOL success) {
       STAssertTrue(success, @"JSON retrieved OK");
       NSArray *postalcodes = [response objectForKey:@"postalcodes"];
       NSDictionary *first = [postalcodes objectAtIndex:0];
       NSString *adminName1 = [first objectForKey:@"adminName1"];
       STAssertTrue([adminName1 isEqualToString:@"New South Wales"], @"NSW");
       done=1;
}];

// https://stackoverflow.com/questions/3615939/wait-for-code-to-finish-execution
while (!done) {
    // This executes another run loop.
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    // Sleep 1/100th sec
    usleep(10000);
}
痞味浪人 2024-11-08 02:33:29

不确定我在哪里找到这个,但有一个更好的方法,不使用睡眠:

while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true) && !placeMarkUpdated){};

这里是在上下文中,测试反向地理编码请求:

__block BOOL placeMarkUpdated = NO;

[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
    if (placeMarkUpdated == NO) {
        placeMarkUpdated = YES;
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        address = [Address addressFromPlacemark:placemark];
    }
}];

while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true) && !placeMarkUpdated){};

睡眠很糟糕,因为它们减慢了构建速度(我知道 5 秒听起来不错,但是想想这个古老的故事:一个人去看医生,因为他的膝盖因跑步而受伤,医生说“站起来”,然后拍拍他的膝盖说“疼吗?”那个人说“不”,医生:“如果我会疼的话”做了一万次……”

Not sure where I found this, but there's a better way that doesn't use sleeps:

while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true) && !placeMarkUpdated){};

here it is in context, testing a reverse geocoding request:

__block BOOL placeMarkUpdated = NO;

[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
    if (placeMarkUpdated == NO) {
        placeMarkUpdated = YES;
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        address = [Address addressFromPlacemark:placemark];
    }
}];

while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true) && !placeMarkUpdated){};

Sleeps suck cause they slow down the build (I know 5s doesn't sound bad, but consider the old story: guy goes to doctor cause his knees hurt from running, Doctor says 'get up on the table' and taps his knee and says 'does that hurt?' guy says 'no,' doctor: 'it would if I did it 10K times...'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文