在为 Objective C 进行单元测试时,如何测试块?
我有一个函数(内部使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
找到了解决方案:
等待代码完成执行
例如:
Found a solution:
Wait for code to finish execution
Eg:
不确定我在哪里找到这个,但有一个更好的方法,不使用睡眠:
这里是在上下文中,测试反向地理编码请求:
睡眠很糟糕,因为它们减慢了构建速度(我知道 5 秒听起来不错,但是想想这个古老的故事:一个人去看医生,因为他的膝盖因跑步而受伤,医生说“站起来”,然后拍拍他的膝盖说“疼吗?”那个人说“不”,医生:“如果我会疼的话”做了一万次……”
Not sure where I found this, but there's a better way that doesn't use sleeps:
here it is in context, testing a reverse geocoding request:
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...'