延迟 OCMock 验证/处理单元测试中的超时
我正在使用 OCMock 测试真实的 Web 服务调用。
现在我正在做一些事情,例如:
- (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 秒。
有没有办法可以设置例如 2 秒的超时,并立即调用 mydelegatemock
的 someMethod
并完成测试用例?
I'm testing real web service calls with OCMock.
Right now I'm doing something like:
- (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];
}
It works fine, but it implies that every such test will take 2 seconds.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用在此链接:
以及实现:
这允许我等待最大延迟(以秒为单位),而无需每次都等待全部延迟。
I do this using a handy utility function I found at this link:
And the implementation:
This allows me to to wait up to a maximum delay (in seconds) without waiting the full amount each time.
我会将 Web 服务的功能测试(如果您确实需要这样做)与处理 Web 服务结果的类的单元测试分开。
要进行单元测试,您应该模拟 Web 服务调用,提供模拟结果。然后,您的测试将验证您的类对于明确定义的结果是否有相应的行为。
如果您还想对 Web 服务进行功能测试(假设它根据某些请求返回特定响应),则无需模拟任何内容 - 只需调用服务并对结果进行断言即可。
通过分离测试,您可以更好地控制测试运行。例如,您可以在每次更改代码时运行快速运行的单元测试,但每晚在专用服务器上或根据需要运行运行缓慢的功能测试。当测试失败时,您就会知道是您的代码出了问题还是 Web 服务出了问题。
I would separate the functional testing of your web services (if you need to do that at all) from the unit testing of your class that processes the web service result.
To unit test, you should mock the web service call, providing a mock result. Then your test would verify that, for that well-defined result, your class behaves accordingly.
If you also want to do functional testing of your web service (say that it returns a specific response given some request), you don't need to mock anything--just call the service and make assertions on the result.
By separating out your tests, you have finer control over the test runs. For example, you could run your fast-running unit tests every time you change code, but run your slow-running functional tests nightly, on a dedicated server, or as needed. And when a test breaks, you'll know whether it's your code or something wrong with the web service.
您也可以切换到 GHUnit,就像一位同事在回答此相关问题时建议的那样:
Xcode 4 中的 SenTestingKit:异步测试?
你可以在这里找到 GHUnit
https://github.com/gabriel/gh-unit
You could also switch to GHUnit, like a fellow member suggests in the answer to this related question:
SenTestingKit in Xcode 4: Asynchronous testing?
You can find GHUnit here
https://github.com/gabriel/gh-unit