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
NARAYANA PANAKALANARAYANA PANAKALA 

How to write Test class for this code which i was attached

trigger c1 on contact (Before Insert){

   try{
            Set<id> AccountIds = new Set<id>();
            map<id,Integer> mapAccountPhone=new map<id,Integer>();
            
            for(contact c : trigger.new){
                AccountIds.add(c.Accounid);              
            }
         for(Account AccC:[select Id,phone from Account where id in : AccountIds])
        {
            mapAccountPhone.put(id,phone);

              }
    
        for(contact c1 : trigger.new){
                c1.phone = mapAccountPhone.get(c1.AccountId); 
                
        }
    }
    Catch(Exception Err)
        {
          System.debug('Exception Occured:'+Err);
        }

}

Hi Team,

Kindly help me with the test class for this code.

Thank you
Agustin BAgustin B
Hi, here you have something:
@IsTest 
 private static void testContact(){
   Account acc = new Account(Name='test acc',Phone = '444');
insert acc;
Contact c = new Contact(LastName='test con',AccountId=acc.id);
insert c;
}
System.assertEquals(acc.Phone,[select Phone from contact where AccountId=acc.id].Phone);
The insert c should execute that trigger you have.

If it helps please mark as correct, it may help others.