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
RossGRossG 

test class for sendEmail trigger

The documentation is not very good on writing a real, effective test class for the sendEmail method.  I've got a trigger that works fine and sends an email through apex, but I'm at a loss for how to write a test class for it:

trigger DocusignStatusTrigger on dsfs__DocuSign_Status__c (after update)
{
    for(dsfs__DocuSign_Status__c ds : Trigger.new){
       if(ds.dsfs__Envelope_Status__c == 'Completed'){
            
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[]{'rosstesta4@gmail.com','rossgilb@gmail.com','rosstesta3@gmail.com'};
            mail.setToAddresses(toAddresses);
            mail.setReplyTo('rgilbert@pingidentity.com');
            mail.setSenderDisplayName('Ping Identity');
            mail.setSubject('A Ping Identity Sales Order has just been signed');
            mail.setBccSender(false);
            mail.setUseSignature(false);
            mail.setPlainTextBody('A Ping Identity Sales Order for '+ ds.Account_Name__c +' has just been signed.  You may view the signed document attached to this email, or view it as a PDF attachment in Salesforce, here: https://cs14.salesforce.com/' + ds.Id+'.  The opportunity record for this envelope can be viewed here: https://cs14.salesforce.com/'+ ds.dsfs__Opportunity__c); 
            mail.setTargetObjectId(ds.OwnerId);
            mail.saveAsActivity = false;       
           
            List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
            
            for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :ds.Id]){
                Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
                efa.setFileName(a.Name);
                efa.setBody(a.Body);
                fileAttachments.add(efa);
            }
            mail.setFileAttachments(fileAttachments);
    
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
        }       
    }
}


Vitap RamdevputraVitap Ramdevputra
Hello RossG,

According to my knowledge. You just have to write a normal test method(in a test class) which updates your custom object dsfs__DocuSign_Status__c.
Something Like this...
@isTest
public class DocusignStatusTrigger_Test{

    public static testMethod void TestAfterUpdateTrigger()
    {
          dsfs__DocuSign_Status__c ds = [select id,dsfs__Envelope_Status__c from dsfs__DocuSign_Status__c limit 1];

           ds.dsfs__DocuSign_Status__c = 'Completed';
           Test.startTest();
           update ds;
           Test.stopTest();
    }
Regards
Vitap Ramdevputra