Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何通过单元测试测试行是否添加到数据库中?_Objective C_Ios_Unit Testing - Fatal编程技术网

Objective c 如何通过单元测试测试行是否添加到数据库中?

Objective c 如何通过单元测试测试行是否添加到数据库中?,objective-c,ios,unit-testing,Objective C,Ios,Unit Testing,我正在将xml传递给AddRowtheDatabase函数。在这个函数中,我只需解析xml文件并将检索到的元素转换为xml文件。现在我正在测试这个应用程序。如何在单元测试用例中测试该行是否添加到数据库中?//我很快就处理了一个查询。。我假设您已经连接到SQLite中的一个DB,所以大部分都是不需要的。但这是查询的基本结构。。只要数据库驻留在应用程序本身中 -(void) basicQuery { @try { NSFileManager *fileManager =

我正在将xml传递给AddRowtheDatabase函数。在这个函数中,我只需解析xml文件并将检索到的元素转换为xml文件。现在我正在测试这个应用程序。如何在单元测试用例中测试该行是否添加到数据库中?

//我很快就处理了一个查询。。我假设您已经连接到SQLite中的一个DB,所以大部分都是不需要的。但这是查询的基本结构。。只要数据库驻留在应用程序本身中

-(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插入和查询,您可能需要运行一个教程。