You need to sign in to do that
Don't have an account?
sagarshah
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
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; }
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:
- Setup of test data. This includes creation of any data needed by your class. Account, Contacts etc
- Starting the test. This is calling Test.startTest() to reset the governor limits
- Calling your class / method
- Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
- Asserting that your changes have worked
- If you have inserted/updated/deleted data, you need to query for the updates
- 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/