gpt4 book ai didi

ios - 使用 Kiwi 测试核心数据实体的后台保存

转载 作者:行者123 更新时间:2023-11-28 20:07:29 24 4
gpt4 key购买 nike

我正在努力找出在后台线程中测试与 Core Data 交互的最佳方法。我有以下类方法:

+ (void)fetchSomeJSON
{
// Download some json then parse it in the block
[[AFHTTPClient sharedClient] fetchAllThingsWithCompletion:^(id results, NSError *error) {
if ([results count] > 0) {

NSManagedObjectContext *backgroundContext = //... create a new context for background insertion
dispatch_queue_t background = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);

dispatch_async(background, ^{ // If I comment this out, my test runs just fine

//... insert and update some entities
for (NSString *str in results) {
NSManagedObject *object = //...
}
});
}
}];
}

我目前正在使用以下 Kiwi 代码测试此方法:

describe(@"MyAction", ^{

__block void (^completionBlock)(NSArray *array, NSError *error);

beforeEach(^{
// Stub the http client
id mockClient = [AFHTTPClient mock];
[WRNAPIClient stub:@selector(sharedClient) andReturn:mockClient];

// capture the block argument
KWCaptureSpy *spy = [mockClient captureArgument:@selector(fetchAllThingsWithCompletion:) atIndex:0];
[MyClass fetchSomeJSON]; // Call the method so we can capture the block
completionBlock = spy.argument;

// run the completion block
completionBlock(@[@"blah"], nil);
})

// If I remove the dispatch_async block, this test passes fine.
// If I add it in again the test fails, probably because its not waiting
it(@"should return the right count", ^{
// entityCount is a block that performs a fetch request count
NSInteger count = entityCount(moc, @"Task");
[[theValue(count) should] equal:theValue(4)];
})

// This works fine, but obviously I don't want to wait a second
it(@"should return the right count after waiting for a second", ^{
sleep(1);
NSInteger count = entityCount(moc, @"Task");
[[theValue(count) should] equal:theValue(4)];
});

};

如果我删除 dispatch_async 行,那么我可以让我的测试快速运行。使用 dispatch_async 时,我可以让我的测试套件运行的唯一方法是在调用完成 block 后 sleep(1)。使用 sleep() 让我觉得我没有以正确的方式接近它。我已经尝试使用 shouldEventually 但这似乎并没有重新获取我的 count 值。

最佳答案

您尝试过这些异步 block 宏吗?

#define TestNeedsToWaitForBlock() __block BOOL blockFinished = NO
#define BlockFinished() blockFinished = YES
#define WaitForBlock() while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true) && !blockFinished)

关于ios - 使用 Kiwi 测试核心数据实体的后台保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21609508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com