Objective c 如何通过单元测试测试行是否添加到数据库中?
我正在将xml传递给AddRowtheDatabase函数。在这个函数中,我只需解析xml文件并将检索到的元素转换为xml文件。现在我正在测试这个应用程序。如何在单元测试用例中测试该行是否添加到数据库中?//我很快就处理了一个查询。。我假设您已经连接到SQLite中的一个DB,所以大部分都是不需要的。但这是查询的基本结构。。只要数据库驻留在应用程序本身中Objective c 如何通过单元测试测试行是否添加到数据库中?,objective-c,ios,unit-testing,Objective C,Ios,Unit Testing,我正在将xml传递给AddRowtheDatabase函数。在这个函数中,我只需解析xml文件并将检索到的元素转换为xml文件。现在我正在测试这个应用程序。如何在单元测试用例中测试该行是否添加到数据库中?//我很快就处理了一个查询。。我假设您已经连接到SQLite中的一个DB,所以大部分都是不需要的。但这是查询的基本结构。。只要数据库驻留在应用程序本身中 -(void) basicQuery { @try { NSFileManager *fileManager =
-(void) basicQuery
{
@try {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *theDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"databasename.sqlite"];
BOOL success = [fileManager fileExistsAtPath:theDBPath];
if (!success) {
NSLog(@"Failed to find database file '%@'.", theDBPath);
}
if (!(sqlite3_open([theDBPath UTF8String], &database) == SQLITE_OK)) {
NSLog(@"An error opening database, normally handle error here.");
}
const char *sql = "SELECT * FROM TABLE"; // your basic query
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) != SQLITE_OK){
NSLog(@"Error, failed to prepare statement, normally handle error here.");
}
while (sqlite3_step(statement) == SQLITE_ROW) {
NSString *value = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
NSLog(@"value = %@",value);
}
if(sqlite3_finalize(statement) != SQLITE_OK){
NSLog(@"Failed to finalize data statement, normally error handling here.");
}
if (sqlite3_close(database) != SQLITE_OK) {
NSLog(@"Failed to close database, normally error handling here.");
}
}
@catch (NSException *e) {
NSLog(@"An exception occurred: %@", [e reason]);
return nil;
}
}
为什么插入后不直接查询数据库?@DJPlayer:您能详细解释一下吗?您使用的是核心数据吗?直接使用Sqlite?还有什么吗?不,我直接使用sqlite,这是一个集成测试,而不是单元测试。单元测试不应该耦合到持久性存储实现。这只是检查异常的一种方法吗?我通常不通过try,catch来做事情。。但数据库连接可能会很尴尬。通常,如果不使用断点和单步检查值,NSLog也可以。如果这是您的第一次DB插入和查询,您可能需要运行一个教程。