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
James BillingsJames Billings 

getEmailInvocations in a test always 0

I have a method in a class which is sending an email out. It works fine, but I want to add a test for it. 

I've created my test, and used the following to check if the email was queued: 

Integer invocations = Limits.getEmailInvocations();
        System.assertEquals(1, invocations);
 

However it always fails, with invocations being 0. I added debug statements to my email-sending method, and these suggest it's "working" from the test (i.e. Messaging.sendEmail returns success). 

One possible cause is that the method that calls the email sending method is a @Future(callout=true) - would this cause the emailinvocations to not get seen in the test? 


 

Raj VakatiRaj Vakati
  1. Did you include them in Test.startTest() and Test.stopTest() 
  2. Are you sending email from the Apex test class as well 

 
James BillingsJames Billings
Thanks Raj- 
1. I've tried it in both places (inside the Start/End test, and outside, but no difference)
2. Not in the test class, no. The class being tested is also a VF Page controller extension which may or may not be relevant too.
James BillingsJames Billings
I found this: https://salesforce.stackexchange.com/questions/252796/test-class-limits-getemailinvocations-showing-zero-and-assertion-is-failing which also suggests that the fact my email call occurs from a future method, that's likely to be the problem...