0

我正在使用 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
4

1 回答 1

1

如果您正在测试CustomerFactory类,那么您根本不应该这样做。把它想象成测试我们单元的接口,它是一个CustomerFactory实例。您有一个名为customersByCategory:的方法,并且您只对从此调用中获取客户对象的 NSArray 感兴趣。您在内部使用DatabaseQueueFMDatabase实例的事实是对这个特定单元测试应该是透明的实现细节。

测试DatabaseQueue类是另一回事,但看起来实现所需目标的最简单方法是在测试中使用DatabaseQueue的真实实例。

于 2013-02-19T10:02:14.103 回答