0

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 = 'test@test.com';
    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?

  • I think it's most likely because of the owner - you're inserting a totally different user in your test method and you're setting it to be the owner of the tasks. Try get the same user in your test method that you're using in the actual code - User Usuario = [Select Id from User where name='Carlos Vanegas' limit 1]; and work with him – Boris Bachovski Oct 26 '17 at 23:25
3

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?

You can't. Users are never isolated in unit tests, so your query will always return the record that already exists, not the one you created.

Instead, query the existing user and use that one. Tasks created for the record you query will not persist beyond the unit test, nor will any existing tasks be pulled into your unit test (because of the test isolation).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.