5

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);
  }

  }
4

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);
}
  • I'm getting an error that beforeInstall Variable does not exist, any suggestions? – McD May 15 '13 at 12:47
  • Hey Seb, any idea on a variable to use besides "beforeInstall"? – McD May 21 '13 at 14:07
  • @McD that is more than strange, what happens if you use a different name for the variable like bfrInstall? – Seb Wagner May 21 '13 at 16:24
  • veery strange, I fooled around with it a bit more and your code worked...user error :) Thanks for your help! – McD May 21 '13 at 21:44
  • Note that you might want to specifically test System.assertEquals( beforeInstall + 1, Limits.getEmailInvocations()). Also note that if you're using Test.startTest(), the assert needs to come before you call Test.stopTest(). – tomlogic Jul 3 '13 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.