异步单元测试不被SenTestCase称为 [英] Asynchronous unit test not being called by SenTestCase
问题描述
当我运行我的单元测试,我没有达到我的断点,我认为测试运行不正常我的code。
我试图做一个异步测试,这是我的困难的一部分。
我如何设置的测试,以便 testLookupBook
将被调用,因为我希望,这将导致一切被正确地调用。
我猜测的功能测试应该是在头文件中的某个地方,但我没有开始我的测试方法与测试
。
下面是我的头文件:
@interface jabBookScanTest:SenTestCase {
jabBookScan *实例;
NSArray的* SearchResult中;
}
@结束
和用于测试的实施是在这里:
- (无效)MethodNameToCallBack:(jabBookScan *)经理
resultFromServer:(NSArray的*)■{
SearchResult中= S;
} - (BOOL)waitForCompletion:(NSTimeInterval)的TimeoutSecs {
*的NSDate = timeoutDate [NSDate的dateWithTimeIntervalSinceNow:的TimeoutSecs];
BOOL做= FALSE;
做{
[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeoutDate];
如果([timeoutDate timeIntervalSinceNow]所述; 0.0)
打破;
}而(!做); 返回完成的;
} - (无效){testLookupBook
*的NSDate = fiveSecondsFromNow [NSDate的dateWithTimeIntervalSinceNow:5.0];
[实例lookupBook:(* NSString的)@9780262140874];
[NSRunLoop currentRunLoop] runUntilDate:fiveSecondsFromNow];
STAssertTrue([个体经营waitForCompletion:90.0] @未能得到任何结果的时间);
}
我会做这样的
- (无效){testLookupBook
*的NSDate = fiveSecondsFromNow [NSDate的dateWithTimeIntervalSinceNow:5.0];
[实例lookupBook:(* NSString的)@9780262140874];
WAIT_WHILE(SearchResult中==为零,1.0);
}
的 WAIT_WHILE(如pressionIsTrue,秒)
-macro直到前pression是不正确的或达到时间限制将评估意见。它的下面是使用NSRunLoop图案。你可以在这里找到项目:
https://github.com/hfossli/AGWaitForAsyncTestHelper
When I am running my unit test, I don't reach my breakpoint, and I think that the test isn't properly running my code.
I am trying to do an asynchronous test, which is part of my difficulty.
How do I set up the test so that testLookupBook
will be called, as I hope that that will lead to everything else being called properly.
I am guessing that the function to test should be somewhere in the header file, but I did start my test method with test
.
Here is my header file:
@interface jabBookScanTest : SenTestCase {
jabBookScan *instance;
NSArray *searchResult;
}
@end
And the implementation for the test is here:
- (void)MethodNameToCallBack:(jabBookScan *)manager
resultFromServer:(NSArray *)s {
searchResult = s;
}
- (BOOL)waitForCompletion:(NSTimeInterval)timeoutSecs {
NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeoutSecs];
BOOL done = false;
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeoutDate];
if([timeoutDate timeIntervalSinceNow] < 0.0)
break;
} while (!done);
return done;
}
- (void)testLookupBook {
NSDate *fiveSecondsFromNow = [NSDate dateWithTimeIntervalSinceNow:5.0];
[instance lookupBook:(NSString *)@"9780262140874"];
[[NSRunLoop currentRunLoop] runUntilDate:fiveSecondsFromNow];
STAssertTrue([self waitForCompletion:90.0], @"Failed to get any results in time");
}
I would do like this
- (void)testLookupBook {
NSDate *fiveSecondsFromNow = [NSDate dateWithTimeIntervalSinceNow:5.0];
[instance lookupBook:(NSString *)@"9780262140874"];
WAIT_WHILE(searchResult == nil, 1.0);
}
The WAIT_WHILE(expressionIsTrue, seconds)
-macro will evaluate the input until the expression is not true or the time limit is reached. Underneath it is using the NSRunLoop-pattern. You can find the project here:
https://github.com/hfossli/AGWaitForAsyncTestHelper
这篇关于异步单元测试不被SenTestCase称为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!