我正在使用 SenTestingKit 和 OCMock 深入研究 TDD 和startet。我使用FMDB作为我的 SQLite 数据库的包装器。
我不知道如何模拟这个类,所以它正确地用一个对象DatabaseQueue
调用了调用块。FMDatabase
有任何想法吗?
客户工厂.h
@interface CustomerFactory
// DatabaseQueue inherits from FMDatabaseQueue
@property (nonatomic, retain) DatabaseQueue *queue;
- (id)initWithDatabaseQueue:(DatabaseQueue *)queue;
@end
客户工厂.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