Note that there are some explanatory texts on larger screens.

plurals
  1. POunit test seems to work but doesn't insert data
    text
    copied!<p>I'm trying to write all the unit test for my DAO layer in objective c for a project I'm working on, but when I run them, everything seems ok while debugging it. For example, I can find the database and execute the query. I know these because I get SQLITE_OK every time I do an operation. Based on this everything looks ok and when I check the database i expect to see de data I'm trying to insert in my test.</p> <p>What I'm trying to do is test the methods that insert a "Rol" in my database.</p> <p>The code looks like these:</p> <p>UnitTest</p> <pre><code>#import "RolTests.h" @implementation RolTests - (void)setUp { [super setUp]; // Set-up code here. } - (void)tearDown { // Tear-down code here. [super tearDown]; } -(void)testInsertarRol { Rol *rol = [[Rol alloc]init]; rol.nombre = @"CEO"; rol.descripcion = @"Master of the universe"; DAORol *dao = [[DAORol alloc]init]; BOOL insertRealizado = [dao insertarRol:rol]; STAssertTrue(insertRealizado, nil); } -(void) obtenerRol{} -(void) obtenerRoles{} -(void)modificarRol{} -(void)testEliminarRol { Rol *rol = [[Rol alloc]init]; rol.identifier = 3; DAORol *dao = [[DAORol alloc]init]; BOOL rolEliminado = [dao eliminarRol:rol]; STAssertTrue(rolEliminado, nil); } @end </code></pre> <p>DAORol</p> <pre><code>#import "DAORol.h" @implementation DAORol -(BOOL)insertarRol:(Rol *) rol { BOOL respuesta; NSString *ubicacionDB = [self obtenerRutaBD]; int b = sqlite3_open_v2([ubicacionDB UTF8String], &amp;bd,SQLITE_OPEN_READWRITE, NULL); if(!(b == SQLITE_OK)){ NSLog(@"No se puede conectar con la BD. Error %i ", b); } const char *sentenciaSQL = [[NSString stringWithFormat:@"insert into Rol (nombre,descripcion) values ('%@', '%@')", rol.nombre, rol.descripcion] UTF8String]; sqlite3_stmt *sqlStatement; int i = sqlite3_prepare_v2(bd, sentenciaSQL, -1, &amp;sqlStatement, NULL); if (i == SQLITE_OK) { respuesta = YES; } if(i != 0){ NSLog(@"Problema al preparar el statement. Error %i ", i); respuesta = NO; } return respuesta; } @end </code></pre> <p>DAOBase</p> <pre><code>#import "DAOBase.h" @implementation DAOBase - (NSString *) obtenerRutaBD{ NSString *dirDocs; NSArray *rutas; NSString *rutaBD; rutas = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); dirDocs = [rutas objectAtIndex:0]; NSFileManager *fileMgr = [NSFileManager defaultManager]; rutaBD = [[NSString alloc] initWithString:[dirDocs stringByAppendingPathComponent:@"totem.sqlite"]]; if([fileMgr fileExistsAtPath:rutaBD] == NO){ [fileMgr copyItemAtPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"totem.sqlite"] toPath:rutaBD error:NULL]; } return rutaBD; } @end </code></pre> <p>EDIT:</p> <p>I change my unit test to look like these</p> <pre><code>-(void)testInsertarRol { Rol *rol = [[Rol alloc]init]; rol.nombre = @"CEO"; rol.descripcion = @"Master of the universe"; DAORol *dao = [[DAORol alloc]init]; BOOL insertRealizado = [dao insertarRol:rol]; NSMutableArray *roles = [dao obtenerRoles]; for (Rol *rol in roles) { NSLog(rol.nombre); } STAssertTrue(insertRealizado, nil); } </code></pre> <p>If you loop roles you would expect to see "CEO" but...you don't...so the data was not inserted in the data base</p> <p>What I expect after running these test is having a row in my Rol table whit the values "CEO" and "Master of the universe"...but the data doesn't get inserted.</p> <p>I Have no idea what is wrong so any pointer would be great.</p> <p>Thanks for your time!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload