问题
If I create a new project which includes unit tests, the test cases get called. But when I'm adding tests to an existing project I'm getting the following in the log:
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)'
started at 2013-05-21 14:11:09 +0000 Test Suite 'testShowContacts' started at 2013-05-21 14:11:09 +0000 Test Suite 'testShowContacts' finished at 2013-05-21 14:11:09 +0000. Executed 0 tests, with 0 failures (0 unexpected) in 0.000 (0.000) seconds
I've added the SenTestKit
to the frameworks, and then added the testing bundle via Add Target.
Where am I going wrong?
Let me add the method and its test case here:
//the method to be tested
-(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);
}
}
}
//here's the test:
-(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");
}
I found that in target dependencies the project was unchecked. I checked it and tested again, whereupon I'm getting the following errors:
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)
I know this is probably going off topic, since the original question was entirely different, but can anyone please tell me why these errors are coming? I've added the necessary frameworks.
回答1:
As you are testing UIKit
dependent stuff, you want to run Application Tests. Make sure you have set the Bundle Loader
and Test Host
build settings (refer to the documentation).
Further you have to link the libsqlite3.0.dylib
against your tests target too, because you are using sqlite functions in your unit tests.
来源:https://stackoverflow.com/questions/16672616/ocunit-test-cases-not-running