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
sagarshahsagarshah 

Write a Test Case for Trigger

Can you let me know how should I begin with and what things I should take into consideration.

Trigger
trigger Timecard on pse__Timecard_Header__c (after insert) {
   
  List<pse__Timecard_Header__c> tc_list = [select Id, pse__End_Date__c, pse__Project__r.pse__Region__r.ffpsai__OwnerCompany__r.Id 
                                             from pse__Timecard_Header__c
                                             where Id IN :Trigger.newMap.KeySet()];

    system.debug('TC_LIST: ' + tc_list);
    
    Set<Date> tc_setofDate = new Set<Date>();
    Set<Id> tc_setofId = new Set<Id>();
 	
  
   	for (pse__Timecard_Header__c tcdate: tc_list)
    	{
       		Date aDate = tcdate.pse__End_Date__c.toStartOfMonth();
           	tc_setofDate.add(aDate);
            system.debug('SET DATE ' + tc_setofDate);
    
    	}
      
    for (pse__Timecard_Header__c tcId: tc_list)
    	{
        	Id ids = tcId.pse__Project__r.pse__Region__r.ffpsai__OwnerCompany__c;
        	tc_setofId.add(ids);
            system.debug('SET ID ' + tc_setofId);
    
    	}
    

    
    List <c2g__codaPeriod__c> periodDetails = [Select Id, c2g__StartDate__c,c2g__EndDate__c, c2g__OwnerCompany__c
                                                from c2g__codaPeriod__c
                                               	where c2g__StartDate__c IN: tc_setofDate
                                              	and c2g__OwnerCompany__c IN: tc_setofId];
           										
           								
       
    system.debug('PERIOD_DETAILS: ' + periodDetails);
 
 	   
    
    List<pse__Timecard_Header__c> tcUpdate = new List<pse__Timecard_Header__c>();
    
        for (pse__Timecard_Header__c timecardCompare: tc_list){
        
        for (c2g__codaPeriod__c periodCompare: periodDetails){
          
            
            if ((timecardCompare.pse__End_Date__c.toStartOfMonth() == periodCompare.c2g__StartDate__c) && 
             	  (timecardCompare.pse__Project__r.pse__Region__r.ffpsai__OwnerCompany__c == periodCompare.c2g__OwnerCompany__c))
            { 
            		
                	timecardCompare.Period__c = periodCompare.Id;
                  	tcUpdate.add(timecardCompare);
                	
                	
          	}
       }
    }	
   
  		update tcUpdate;
}

 
pconpcon
Since writing a test class to cover all of the facets of this class is not something that anyone on here will do for you, I can give you some pointers and hopefully get you started.  I would recommend that you do some reading on testing [1] [2] [3] to get a better understanding.  Each of your individual tests should only tests one specific portion of you class (ie a constructor test, sendEmail test, contactSelected test, etc).  You should also have both a postitive (everything works perfectly) and a negative (things are not right) test. 

In addition, since this is a test for a trigger i would also make sure you have a test that covers bulk testing.  This is where you would insert 200+ instances of pse__Timecard_Header__c in a single insert and make sure that you do not hit any governor limits.

Each test should follow the following structure:
  1. Setup of test data. This includes creation of any data needed by your class.  Account, Contacts etc
  2. Starting the test. This is calling Test.startTest() to reset the governor limits
  3. Calling your class / method
  4. Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
  5. Asserting that your changes have worked
    1. If you have inserted/updated/deleted data, you need to query for the updates
    2. Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.

[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/