延迟 OCMock 验证/处理单元测试中的超时

发布于 2024-12-03 04:36:59 字数 595 浏览 1 评论 0原文

我正在使用 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 秒的超时,并立即调用 mydelegatemocksomeMethod 并完成测试用例?

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 技术交流群。

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

发布评论

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

评论(3

亚希 2024-12-10 04:36:59

我使用在此链接:

#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

这允许我等待最大延迟(以秒为单位),而无需每次都等待全部延迟。

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

And the implementation:

#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.

二智少女 2024-12-10 04:36:59

我会将 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.

无声无音无过去 2024-12-10 04:36:59

您也可以切换到 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

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