function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rakshith RamachandraRakshith Ramachandra 

Help with the Test class for the following trigger.

Below trigger updates the Employee count under contact record whenever the employee count under Task it's task is updated.
trigger Update_employee_count_in_activity on Task (after insert, after update) {
	List<Contact> contsToUpdate = new List<Contact>();
	Schema.DescribeSObjectResult r = Contact.sObjectType.getDescribe();
	String keyPrefix = r.getKeyPrefix();
	for(Task t:trigger.new){
		String myIdPrefix = String.valueOf( t.WhoId).substring(0,3);
		if(myIdPrefix == keyPrefix){
			Contact cont = new Contact();
			cont.Id = t.WhoId;
			cont.Employees__c = t.FT_Emp__c;
			contsToUpdate.add(cont);
		}
	}
	if (contsToUpdate.size() > 0)
    {
        update contsToUpdate;
    }
}

Can anyone help me with the test class for this trigger? Thanks
Best Answer chosen by Rakshith Ramachandra
Deepak GulianDeepak Gulian
@isTest 
public class UpldateEmployeeCountsTest 
{
    static testMethod void testMethod1() 
	{
		Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		insert testAccount;

               Contact con = new Contact();
               con.LastName = 'Lname';
               con.FirstName = 'Fname';
               con.AccountID = testAccount.Id;
               con.Employees__c = 0;
               insert con;

              Task tk = new Task();
              tk.Status = 'Not Started';
              tk.FT_Emp__c = 2;
              tk.WhoId = con.Id;
              tk.Subject = 'Other';
              tk.Priority='High';
              insert tk;


             //Update Task
              Task tkUp = [Select Id,FT_Emp__c From Task Where Id  =:tk.Id];    
              tkUp.FT_Emp__c = 5
              update tkUp;

             system.assertEquals(5,con.Employees__c);
    }
}

All Answers

R Z KhanR Z Khan
in your method. create a contact. then create a task with FT_Emp__c field populated. isnert it. then query the contact adn assert that Employee__c field was populated correctly
Deepak GulianDeepak Gulian
@isTest 
public class UpldateEmployeeCountsTest 
{
    static testMethod void testMethod1() 
	{
		Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		insert testAccount;

               Contact con = new Contact();
               con.LastName = 'Lname';
               con.FirstName = 'Fname';
               con.AccountID = testAccount.Id;
               con.Employees__c = 0;
               insert con;

              Task tk = new Task();
              tk.Status = 'Not Started';
              tk.FT_Emp__c = 2;
              tk.WhoId = con.Id;
              tk.Subject = 'Other';
              tk.Priority='High';
              insert tk;


             //Update Task
              Task tkUp = [Select Id,FT_Emp__c From Task Where Id  =:tk.Id];    
              tkUp.FT_Emp__c = 5
              update tkUp;

             system.assertEquals(5,con.Employees__c);
    }
}
This was selected as the best answer
R Z KhanR Z Khan
@isTest 
public class UpldateEmployeeCountsTest 
{
    static testMethod void testMethod1() 
	{
		Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		insert testAccount;

               Contact con = new Contact();
               con.LastName = 'Lname';
               con.AccountID = testAccount.Id;
               con.Employees__c = 0;
               insert con;

              Task tk = new Task();
              tk.Status = 'Not Started';
              tk.FT_Emp__c = 2;
              tk.WhoId = con.Id;
              tk.Subject = 'Other';
              tk.Priority='High';
              insert tk;

             con = [Select Id,Employees__c From Contact];    
  system.assertEquals(tk.FT_Emp__c,con.Employees__c);

             //Update Task
              tkUp.FT_Emp__c = 5
              update tkUp;

             con = [Select Id,Employees__c From Contact];    
  system.assertEquals(tk.FT_Emp__c,con.Employees__c);
    }
}

 
R Z KhanR Z Khan
In the Contact query add LIMIT 1. so it would assign the result to 1 record
Rakshith RamachandraRakshith Ramachandra
Worked perfectly after debugging.
At first Account record was missing. Then added new inserted new account as per Deepak's suggestion.
The system assert was failing.
Then changed the system assert as per RZ's suggestion. It worked.
Thanks you both.