MochaAsyncTest 0.2.2

MochaAsyncTest 0.2.2

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布时间最后发布时间2014年12月

azuakuraru 维护。



安装

用法

@interface MochaAsyncTest : NSObject
+ (void)runBlock:(void (^)(MochaAsyncDone done, MochaAsyncDoneWithError error)) runTestBlock;

+ (void)runBlock:(void (^)(MochaAsyncDone, MochaAsyncDoneWithError)) runTestBlock timeout:(NSTimeInterval) timeout;
@end

您可以使用它进行异步测试。

[MochaAsyncTest runBlock:^(MochaAsyncDone done, MochaAsyncDoneWithError error) {
    /*
     async function
    */
    done();
    // or
    error(error);
}];

完成

通过测试时,您必须调用 done()

失败

如果测试失败,您必须调用 fail(NSError *error)

describe(@"MochaAsyncTest", ^{
    it(@"pass test", ^{
        [MochaAsyncTest runBlock:^(MochaAsyncDone done, MochaAsyncDoneWithError error) {
            // async function
            double delayInSeconds = 2.0;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            dispatch_after(popTime, queue, ^(void) {
                done();// => done async test
            });
        }];
    });
    it(@"fail test", ^{
        [MochaAsyncTest runBlock:^(MochaAsyncDone done, MochaAsyncDoneWithError error) {
            // async function
            double delayInSeconds = 2.0;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            dispatch_after(popTime, queue, ^(void) {
                // fail
                fail([[NSError alloc] initWithDomain:@"xxx" code:0 userInfo:@{}]);
            });
        }];
    });

});

已知问题

当使用 Kiwi 和 error(NSError *error) 时,虽然测试失败,但会抛出异常...

2014-08-29 15:52:25.523 MochaAsyncTest[21402:1003] Fail Error: Error Domain=xxx Code=0 "The operation couldn’t be completed. (xxx error 0.)"
2014-08-29 15:52:25.523 MochaAsyncTest[21402:1003] +[MochaAsyncTest recordFailureWithDescription:inFile:atLine:expected:]: unrecognized selector sent to class 0x9461b4c
2014-08-29 15:52:25.524 MochaAsyncTest[21402:1003] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[MochaAsyncTest recordFailureWithDescription:inFile:atLine:expected:]: unrecognized selector sent to class 0x9461b4c'
*** First throw call stack:
(
    0   CoreFoundation                      0x017f11e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x015708e5 objc_exception_throw + 44
    2   CoreFoundation                      0x0188e0e3 +[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x017e150b ___forwarding___ + 1019
    4   CoreFoundation                      0x017e10ee _CF_forwarding_prep_0 + 14
    5   XCTest                              0x2010444a _XCTFailureHandler + 1050
    6   Tests                               0x0944272f __35+[MochaAsyncTest runBlock:timeout:]_block_invoke3 + 303
    7   Tests                               0x094021fb __34+[InitialTests buildExampleGroups]_block_invoke_314 + 235
    8   libdispatch.dylib                   0x0256e7b8 _dispatch_call_block_and_release + 15
    9   libdispatch.dylib                   0x025834d0 _dispatch_client_callout + 14
    10  libdispatch.dylib                   0x025709c6 _dispatch_after_timer_callback + 98
    11  libdispatch.dylib                   0x025834d0 _dispatch_client_callout + 14
    12  libdispatch.dylib                   0x02579e93 _dispatch_source_latch_and_call + 150
    13  libdispatch.dylib                   0x0257443f _dispatch_source_invoke + 415
    14  libdispatch.dylib                   0x02571de2 _dispatch_root_queue_drain + 78
    15  libdispatch.dylib                   0x02572127 _dispatch_worker_thread2 + 39
    16  libsystem_pthread.dylib             0x028b2dab _pthread_wqthread + 336
    17  libsystem_pthread.dylib             0x028b6cce start_wqthread + 30
)
libc++abi.dylib: terminating with uncaught exception of type NSException

要求

作者

azu,[email protected]

许可

MochaAsyncTest 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。