我正在测试这种方法:
-(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];
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
{
testString = [[NSMutableString alloc]init];
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 = @"5";
[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"];
const char *query_stmt = [selectSQL UTF8String];
if(sqlite3_prepare_v2(mDiaryTest,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while(sqlite3_step(statement) == SQLITE_ROW)
{
testString = [[NSMutableString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
if([testString isEqualToString:obj.mTaskName.text])
{
break;
}
NSLog(@"%@",testString);
}
}
}
sqlite3_finalize(statement);
sqlite3_close(mDiaryTest);
NSLog(@"%@",testString);
STAssertEquals(testString,obj.mTaskDescription.text,@"should be equal");
// STAssertNotNil(testString,@"Not nil");
}
addTask方法应该在我的数据库中添加一个任务。为了测试这个方法,我创建了一个AddTaskViewController的对象,addTask所在的对象,并传递了一些虚拟值。但是,当我从测试用例中调用addContact方法时,这些值无法识别。因此,它无法在数据库中插入值。我该如何解决这个问题?请帮忙