私は TDD を掘り下げており、 SenTestingKit とOCMockを使用して開始しています。SQLite データベースのラッパーとしてFMDBを使用しています。
クラスをモックする方法について頭を悩ませることができないためDatabaseQueue
、オブジェクトを使用して呼び出しブロックを正しく呼び出しFMDatabase
ます。
何か案は?
CustomerFactory.h
@interface CustomerFactory
// DatabaseQueue inherits from FMDatabaseQueue
@property (nonatomic, retain) DatabaseQueue *queue;
- (id)initWithDatabaseQueue:(DatabaseQueue *)queue;
@end
CustomerFactory.m
@implement CustomerFactory
- (id)initWithDatabaseQueue:(DatabaseQueue *)queue
{
if ((self = [super init]))
{
[self setQueue:queue];
}
}
- (NSArray *)customersByCategory:(NSUInteger)categoryId
{
__block NSMutableArray *temp = [[NSMutableArray alloc] init];
[self.queue inDatabase:^(FMDatabase *db)
{
FMResultSet *result = [db executeQuery:@"SELECT * FROM customers WHERE category_id = ?", categoryId];
while ([result next])
{
Customer *customer = [[Customer alloc] initWithDictionary:[result resultDictionary]];
[temp addObject:customer;
}
}];
return temp;
}
@end