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
HARSHIL U PARIKHHARSHIL U PARIKH 

Trigger (Small one) works but little trouble with test cases

Hello Developers!
I have a simple trigger which is working fine but I have little trouble in test cases:
Few things to consider:
Master Object: Volunteer_Project__c
Child Object: Participation__c
Trigger:
Trigger UniqueEmailCount On Volunteer_Project__c(before insert, before update)
{
    List<Participation__c> ParticipationList = new List<Participation__c>();
    Set<String> uniqueEmails = new Set<String>();
    
    ParticipationList = [SELECT Volunteer_Email__c FROM Participation__c where Volunteer_Project_Name__c IN :Trigger.New];

        for (Integer i = 0; i< ParticipationList.size(); i++)
        {
            uniqueEmails.add(ParticipationList[i].Volunteer_Email__c);
          
        }
        
        for(Volunteer_Project__c proj: Trigger.new)
        {
            proj.Total_Associated_Volunteers__c = uniqueEmails.size();
        }
}
Test Cases: (NOT GETTING ANY COVERAGE)
@isTest
Public class UniqueEmailCount_Test{
    Public Static TestMethod Void TestingUniqueEmailCount(){
        
        Set<String> uniqueEmails = new Set<String>();
        List<Participation__c> partiList = new List<Participation__c >();
        for(Integer I = 0; I < 200; I++)
        {
            Participation__c Parti = New Participation__c (Volunteer_Name__c = 'Person:' + i, Volunteer_Project_Name__c = 'Project:' + i
                                                , Devoted_Hours__c = 1);   
            partiList.add(Parti);
            
        }
       insert partiList;
        
        for (Integer k = 0; k< partiList.size(); k++)
        {
            uniqueEmails.add(partiList[k].Volunteer_Email__c);
          
        }
        
        List<Volunteer_Project__c> vp = new List<Volunteer_Project__c>();
        vp = [Select Total_Associated_Volunteers__c from Volunteer_Project__c];
         
         for (Volunteer_Project__c vp1: vp ){
          // system.assetEquls(vp1.Total_Associated_Volunteers__c, uniqueEmails.size());
         }
        
    }

}
Note: Volunteer_Email__c is a formula field on child Participation__c. This field is catching emails from contacts.

 
Best Answer chosen by HARSHIL U PARIKH
badibadi
In your test class you have not populated Volunteer_Email__c field when you are inserting Participation__c records. 

All Answers

badibadi
In your test class you have not populated Volunteer_Email__c field when you are inserting Participation__c records. 
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
You need to insert or update Volunteer_Project__c object record.

Please try below test class.
@isTest
Public class UniqueEmailCount_Test{
    Public Static TestMethod Void TestingUniqueEmailCount()
	{
        
        Set<String> uniqueEmails = new Set<String>();
        List<Participation__c> partiList = new List<Participation__c >();
        for(Integer I = 0; I < 200; I++)
        {
            Participation__c Parti = New Participation__c (Volunteer_Name__c = 'Person:' + i, Volunteer_Project_Name__c = 'Project:' + i
                                                , Devoted_Hours__c = 1);   
            partiList.add(Parti);
        }
		insert partiList;
        
        for (Integer k = 0; k< partiList.size(); k++)
        {
            uniqueEmails.add(partiList[k].Volunteer_Email__c);
        }
		
		Volunteer_Project__c volProj = new Volunteer_Project__c();
		// add all required field here
		insert 	volProj;
        
    }

}

Let us know if this will help you
HARSHIL U PARIKHHARSHIL U PARIKH
Hi Badi and Amit Chaudhary 8,  thanks both of you for the replay.
I have added one more thing in trigger.
@isTest
Public class UniqueEmailCount_Test{
    Public Static TestMethod Void TestingUniqueEmailCount()
    {
        
        Set<String> uniqueEmails = new Set<String>();
        List<Participation__c> partiList = new List<Participation__c >();
        List<Volunteer_Project__c> Vprojects = new List<Volunteer_Project__c >();
        List<Contact> Cons = New List<Contact>();
        
        for(Integer L = 0; L < 200 ; L++){
        Contact C = New Contact(FirstName = 'John' + L, LastName = 'Doe' + L, Email = 'John.Doe@gmail.com' + L);
        Cons.add(C);
        }
        insert Cons;
        
        for(Integer I = 0; I < 200; I++)
        {
            Participation__c Parti = New Participation__c (Volunteer_Name__c = 'Person:' + i, Volunteer_Project_Name__c = 'Project:' + i
                                                , Devoted_Hours__c = 1 );   
            partiList.add(Parti);
        }
        insert partiList;
        
        for (Integer k = 0; k< partiList.size(); k++)
        {
            uniqueEmails.add(partiList[k].Volunteer_Email__c);
        }
        
        Volunteer_Project__c volProj = new Volunteer_Project__c(); 
        // add all required field here 
         insert volProj;
        
    }

}
But I was unable to understand how to add those field above. Well, these are all fields for Volunteer_Project__c
User-added image
Thank you for the followup guys..
 
HARSHIL U PARIKHHARSHIL U PARIKH
It;s working fine guys,.. Thank you!