While running test class.. I am continuously getting this error
Invalid type: Schema.SingleEmailMessage
#Code:-
@isTest
public class TestContactEmailTrigger {
@isTest
public static void testTrigger() {
// Create test data
List<Contact> contacts = new List<Contact>();
for (Integer i = 0; i < 5; i++) {
Contact contact = new Contact(
LastName = 'TestContact' + i,
Email = 'test' + i + '@example.com',
Message_sent__c = true
);
contacts.add(contact);
}
insert contacts;
// Perform DML operation to trigger the email sending
Test.startTest();
for (Contact contact : contacts) {
contact.Message_sent__c = true;
}
update contacts;
Test.stopTest();
// Verify the emails are sent correctly
List<Messaging.SingleEmailMessage> sentEmails = [SELECT Id, ToAddress, Subject, PlainTextBody FROM Messaging.SingleEmailMessage];
System.assertEquals(5, sentEmails.size());
// Check the content of the email
for (Integer i = 0; i < 5; i++) {
Messaging.SingleEmailMessage email = sentEmails[i];
Contact contact = contacts[i];
System.assertEquals(contact.Email, email.getToAddresses()[0]);
System.assertEquals('Important Update', email.getSubject());
System.assertEquals('Hello, \n\nThis is an important update regarding your contact information.', email.getPlainTextBody());
}
}
Hello, i am currently having the same issue, i keep receiving the error : Invalid type: Schema.SingleEmailMessage . have you solved you problem? or is it that we can't access the sent email messages in test class as they are not stored within salesforce?