Help with PostInstall Test Method Assert

I’m having trouble adding an Assert to the Test Method in my PostInstall Email Class. Can anyone point me to a resource or identify where I’m going wrong in my code? Thanks!

global class PostInstallEmail implements InstallHandler {
  global void onInstall(InstallContext context) {
    if(context.previousVersion() == null) {

      User u = [Select Id, Email from User where Id =:context.installerID()];   
      String toAddress= u.Email;
      String[] toAddresses = new String[]{toAddress};
      Messaging.SingleEmailMessage mail = 
        new Messaging.SingleEmailMessage();
      mail.setToAddresses(toAddresses);
      mail.setReplyTo('noone@gmail.com');
      mail.setSenderDisplayName('App Support');
      mail.setSubject('Complete Your App Installation');
      mail.setPlainTextBody('Thanks for installing my app!');
      Messaging.sendEmail(new Messaging.Email[] { mail });
      }
    }


    @isTest
    static void testInstallScript() {
        PostInstallEmail postinstall = new PostInstallEmail ();
        Test.testInstall(postinstall, null);
  }

  }

Answer

I’m not sure if limits are available in InstallHandler, but it’s worth a try to check Limits.getEmailInvocations() before and after.

@isTest
static void testInstallScript() {
    Integer beforeInstall = Limits.getEmailInvocations();
    PostInstallEmail postinstall = new PostInstallEmail ();
    Test.testInstall(postinstall, null);
    system.assert(Limits.getEmailInvocations() > beforeInstall);
}

Attribution
Source : Link , Question Author : McD , Answer Author : tomlogic

Leave a Comment