I'm having trouble with my code coverage and have a couple of hypothesis why this could be.
My class:
public class Tareas_Ordenador {
public static void Ordenar(){
User Usuario = [Select Id from User where name='Carlos Vanegas' limit 1];
list <task> Tareas_abiertas = [SELECT Id, ActivityDate, status, OwnerId from Task where OwnerId =:usuario.id AND status='Open' and ActivityDate<:Date.today() Order By ActivityDate];
integer i =0;
datetime dia = Date.today()+2;
string num_dia = dia.format('u');
date dias = Date.today()+2;
if (num_dia=='5'){
dia = dia + 2;
dias = dias + 1;
} else if (num_dia=='6'){
dia = dia + 1;
dias = dias + 1;
}
For(Task tas : Tareas_abiertas){
tas.ActivityDate = dias;
i++;
if(i>15){
dia=dia+1;
dias=dias+1;
i=0;
num_dia = dia.format('u');
if (num_dia=='5'){
dia = dia + 2 ;
dias = dias + 1;
} else if (num_dia=='6'){
dia = dia + 1;
dias = dias + 1;
}
}
}
update Tareas_Abiertas;
}
}
My test class:
@istest (seealldata=false) private class Tareas_Ordenador_isTest {
@istest public static void test_Ordenador() {
user u = new user();
u.LastName = 'Vanegas';
u.FirstName = 'Carlos';
u.Email = '[email protected]';
u.Alias = 'Tcode';
u.Username = Crypto.getRandomLong()+'@'+Crypto.getRandomLong()+'.abc';
u.CommunityNickname = 'test12';
u.LocaleSidKey = 'en_US';
u.TimeZoneSidKey = 'GMT';
u.ProfileID = '00e36000000ao2F';
u.LanguageLocaleKey = 'en_US';
u.EmailEncodingKey = 'UTF-8';
insert u;
List<task> tas = new list<task>();
for(integer i=0;i<17;i++){
task ta = new task();
ta.ActivityDate = Date.today()-5;
ta.OwnerId= u.id;
ta.Status='Open';
tas.add(ta);
}
insert tas;
Tareas_Ordenador.Ordenar();
}
}
The issue is that the list of tasks named Tareas_Abiertas is empty after the query. I have two explanations as to why this could be but no idea how to solve them.
There already exists a user in my org named Carlos Vanegas and the query is returning that user instead of the one I created on the test class so when I query for tasks none are returned. How can I limit the scope of the class only to the data created in the test class?
I'm doing something wrong with a DML expression and it's not inserting the tasks correctly. What could the error be?
User Usuario = [Select Id from User where name='Carlos Vanegas' limit 1];
and work with him