+ Start a Discussion
Adaniels117Adaniels117 

Help with Test Class on Trigger

Hey guys, I'm having trouble writing a test class for this trigger. Its my first trigger, works in Sandbox...just not sure where to start with creating a test class so I can successfully deploy in production. Any Guidance would be incredibly helpful:

 

trigger HasSoldTask on Task(after update, after insert)
{
List<Id> accIds = new List<Id>();
   for(Task t : Trigger.new)
    {
        If (t.status=='sold')
        {
            accids.add(t.accountid);
        }
    }
   Map<Id, Account> accMap = new Map<Id, Account>([SELECT Id, Has_Sold_Opp__c FROM Account WHERE Id IN :accIds]);
    
    Map<Id, Account> updateMap = new Map<Id, Account>();
    
    for (Task t: Trigger.new)
    {
        If (t.Accountid <> null && (t.Status == 'sold') && (accMap.get(t.AccountId).Has_Sold_Opp__c < t.LastModifiedDate || accMap.get(t.AccountId).Has_Sold_Opp__c ==null))
        {
            Account acc = accMap.get(t.AccountId);
            acc.Has_Sold_Opp__c  = t.LastModifiedDate;
            updateMap.put(t.AccountId, acc);
        }
    }
    update updateMap.values();
    }

 What should I be testing for? Where do I start?

ManjunathManjunath

Hi,

 

Test class is basically a class which simulates the users behavior (Only thing it needs to be done programmatic :) ).

 

To write a test class.

1: @istest annotation

2. Create a dummy day which satisfies triggers condition condition Ex: In your case task created for testing should have Status == 'sold' etc.

3. Between test.starttest() and test.stoptest() do your dml operation. Such as inserting the dummy record created for testing.

 

Hope this help.

 

Regards,