0

私は 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
4

1 に答える 1

1

CustomerFactoryクラスをテストしている場合は、まったくテストしないでください。これは、 CustomerFactoryインスタンスであるユニットのインターフェースをテストするものと考えてください。CustomersByCategory:というメソッドがあり、この呼び出しからcustomersオブジェクトのNSArrayを取得することにのみ関心があります。内部でDatabaseQueueインスタンスとFMDatabaseインスタンスを使用するという事実は、この特定の単体テストに対して透過的である必要がある実装の詳細です。

DatabaseQueueクラスのテストは別の話ですが、目的を達成するための最も簡単な方法は、テストでDatabaseQueueの実際のインスタンスを使用することです。

于 2013-02-19T10:02:14.103 に答える