0

如果我创建一个包含单元测试的新项目,则会调用测试用例。但是当我向现有项目添加测试时,我在日志中得到以下信息:

2013-05-21 19:41:08.814 otest[51593:7e03] Unknown Device Type. Using UIUserInterfaceIdiomPhone based on screen size
Test Suite '/Users/impadmin/Library/Developer/Xcode/DerivedData/SmartDiary-frrybdtgyyjgewbialqsmxkwvlxs/Build/Products/Debug-iphonesimulator/testSmartDiary.octest(Tests)'

开始于 2013-05-21 14:11:09 +0000 测试套件“testShowContacts”开始于 2013-05-21 14:11:09 +0000 测试套件“testShowContacts”完成于 2013-05-21 14:11:09 +0000。在 0.000 (0.000) 秒内执行 0 次测试,0 次失败(0 次意外)

我已将其添加SenTestKit到框架中,然后通过添加目标添加了测试包。我哪里错了?

让我在这里添加方法及其测试用例:

//要测试的方法

-(IBAction)addTask{
    if(!([mPriority.text isEqualToString:@"1"] ||[mPriority.text isEqualToString:@"2"] ||[mPriority.text isEqualToString:@"3"] ||[mPriority.text isEqualToString:@"4"] ||[mPriority.text isEqualToString:@"5"]))
    {
        NSLog(@"Enter valid Priority value");
    }
    else if ([mTaskName.text isEqualToString:@""]) {
            NSLog(@"Task name can't be blank");
        }
        else{
    sqlite3_stmt    *statement;
    const char *dbPath = [mDatabasePath UTF8String];
    //[[appViewController returnObject].mDatabasePath UTF8String];

    if (sqlite3_open(dbPath, &mDiary) == SQLITE_OK)
    {
        NSLog(@"%@",mPriority.text);

        NSString *insertSQL2=[NSString stringWithFormat:@"INSERT INTO TODO VALUES(\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")",mStartDate.text,mEndDate.text,mTaskName.text,mTaskDescription.text,mPriority.text];
        NSLog(@"%@",insertSQL2);
        const char *insert_stmt2 = [insertSQL2 UTF8String];
        sqlite3_prepare_v2(mDiary, insert_stmt2,
                           -1, &statement, NULL);
        NSLog(@"%@",mPriority.text);
        if (sqlite3_step(statement) == SQLITE_DONE )
        { 
            mStartDate.text=@"";
            mEndDate.text=@"";
            mTaskName.text=@"";
            mTaskDescription.text=@"";
            mPriority.text=@"";
        }
        else {

            NSLog(@"failed to add");
        }
        sqlite3_finalize(statement);
        sqlite3_close(mDiary);
        }
    }


}

//这里是测试:

-(void)testAddTask
{
    AddTaskViewController *obj;
    obj = [[AddTaskViewController alloc]init];
    obj.mTaskName.text = @"t1";
    obj.mTaskDescription.text = @"t2";
    obj.mStartDate.text = @"56987";
    obj.mEndDate.text = @"55425";
    obj.mPriority.text = @"3";

    [obj addTask];
    sqlite3_stmt *statement;
    const char *dbpath = [obj.mDatabasePath UTF8String];
    if (sqlite3_open(dbpath,&mDiaryTest)== SQLITE_OK) {
        NSString *selectSQL = [NSString stringWithFormat:@"SELECT * FROM TODO WHERE mTaskName = \"%@\"",obj.mTaskName.text];
        const char *query_stmt = [selectSQL UTF8String];
        if (sqlite3_prepare_v2(mDiaryTest,
                               query_stmt, -1, &statement, NULL) == SQLITE_OK)
        {
            if(sqlite3_step(statement) == SQLITE_ROW)
            {
                testString = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
            }
        }
    }
    sqlite3_finalize(statement);
    sqlite3_close(mDiaryTest);
    NSLog(@"%@",testString);
    STAssertEquals(testString,@"t2",@"should be equal");

}

我发现在目标依赖项中,该项​​目未选中。我检查了它并再次测试,因此我收到以下错误:

Undefined symbols for architecture i386:
  "_OBJC_CLASS_$_AddTaskViewController", referenced from:
      objc-class-ref in testAddTask.o
  "_sqlite3_close", referenced from:
      -[testAddTask testAddTask] in testAddTask.o
  "_sqlite3_column_text", referenced from:
      -[testAddTask testAddTask] in testAddTask.o
  "_sqlite3_finalize", referenced from:
      -[testAddTask testAddTask] in testAddTask.o
  "_sqlite3_open", referenced from:
      -[testAddTask testAddTask] in testAddTask.o
  "_sqlite3_prepare_v2", referenced from:
      -[testAddTask testAddTask] in testAddTask.o
  "_sqlite3_step", referenced from:
      -[testAddTask testAddTask] in testAddTask.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我知道这可能离题了,因为最初的问题完全不同,但是谁能告诉我为什么会出现这些错误?我已经添加了必要的框架。

4

1 回答 1

0

当你测试UIKit依赖的东西时,你想运行Application Tests。确保您已设置Bundle LoaderTest Host构建设置(请参阅文档)。

此外,您还必须将libsqlite3.0.dylib您的测试目标链接起来,因为您在单元测试中使用了 sqlite 函数。

于 2013-05-21T16:50:35.370 回答