Let me begin by saying, I know that this could be significantly better code if using the Trigger.newMap and Trigger.oldMap I have run into a situation though where I need to reuse a class within a trigger that uses a contstructor to pass in the maps from the trigger --> triggerHandler --> Class w/ Contstructor
I have created a void method to handle a simple update of subscription preferences. I was able to get the Insert version to work and have full coverage there, however I am not able to get coverage on the comparison between old values and new values in the trigger. It will hit the conditionals but not actually enter into the conditional. When I attempt to do an assert on the subscription values, it fails. I'm not sure what I'm missing.
ContactProcess.cls
// Constructor called in trigger handler.
public class ContactProcesses {
public enum Operation_Mod { BEFORE_OP, AFTER_OP }
private List<Contact> givenContactList;
private Map<Id, Contact> oldContactMap;
private Map<Id, Contact> newContactMap;
private List<Contact> updateContactList;
private Contact_Trigger_Setting__mdt triggerSettings;
Operation_Mod mod;
public ContactProcesses( Map<ID, Contact> newCons, Map<ID, Contact> oldCons, Contact_Trigger_Setting__mdt settings, Operation_Mod mod) {
updateContactList = new List<Contact>();
toCreateTaskLst = new List<Task>();
updateAccountList = new List<Account>();
updateMemDetails = new List<CA_Membership_Details__c>();
classificationsToCreate = new List<Classification_Type_Contact__c>();
givenContactList = newCons.values();
oldContactMap = oldCons;
newContactMap = newCons;
triggerSettings = settings;
this.mod = mod;
}
//my Method that I'm trying to cover:
public void subscriptionPrefsUpdate( Map<String, Contact_Subscription_Recipes__mdt> mapOfRecipes){
System.debug('SubscriptionMethod Active');
for (Contact c : newContactMap.values()) {
Contact oldc = oldContactMap.get(c.Id);
System.debug('NewContact: ' + String.valueOf(c.Recipe__c));
System.debug('OldContact: ' + String.valueOf(oldc.Recipe__c));
if (c.Recipe__c != null && oldc.Recipe__c != c.Recipe__c) {
Contact_Subscription_Recipes__mdt subRecipe = mapOfRecipes.get('Recipe_' + c.Recipe__c); //subscription recipe
c.AMFEMs_Opt_Out__c = subRecipe.meta_MFEMsOptOut__c;
c.Appeals_Opt_Out__c = subRecipe.meta_MonthlyAppealLetter_ML_OptOut__c;
c.Do_Not_Mail__c = subRecipe.meta_DoNotMail__c;
} else if(c.Recipe__c == null && oldc.Recipe__c != c.Recipe__c) {
Contact_Subscription_Recipes__mdt subRecipe = mapOfRecipes.get('Recipe_None');
c.AMFEMs_Opt_Out__c = subRecipe.meta_MFEMsOptOut__c;
c.Appeals_Opt_Out__c = subRecipe.meta_MonthlyAppealLetter_ML_OptOut__c;
c.Do_Not_Mail__c = subRecipe.meta_DoNotMail__c;
} else if(oldc.Recipe__c == c.Recipe__c && c.Recipe__c != null) {
validateSubscriptionValues(oldc, c, triggerSettings);
}
UnitTestClass
@isTest
private static void testBeforeUpdateContactRecipesPreferences(){
//Test Data
Contact_Trigger_Setting__mdt triggerSettings = [SELECT Id,TurnOnContactTriggerErrors__c FROM Contact_Trigger_Setting__mdt WHERE DeveloperName = 'Contact_Trigger_Settings'];
Map<String, Contact_Subscription_Recipes__mdt> mapOfRecipes = ContactGateWay_AC.getRecipesMap();
Map<Id, Contact> newContacts = new Map<Id,Contact>();
Map<Id,Contact> oldContacts = new Map<Id, Contact>();
Contact cOld = new Contact(
FirstName = 'TestFirst',
LastName = 'TestLast',
Recipe__c ='1',
Email = '[email protected]',
);
insert cOld;
System.assertEquals('1', cOld.Recipe__c);
System.assertEquals(true, cOld.Do_Not_Mail__c);
oldContacts.put(cOld.Id, cOld);
Contact cNew = new Contact();
cNew = cOld;
cNew.Recipe__c = '2';
newContacts.put(cNew.Id, cNew);
update cNew;
ContactProcesses cp = new ContactProcesses(newContacts, oldContacts, triggerSettings, ContactProcesses.Operation_Mod.BEFORE_OP);
cp.subscriptionPrefsUpdate(mapOfRecipes);
Contact savedCon = [
SELECT
Id,
Recipe__c,
ADF_Foundation_Mail_Opt_Out__c,
AMFEMs_Opt_Out__c,
Appeals_Opt_Out__c,
Do_Not_Mail__c,
Faith_Justice__c,
Newsletter_NL_Opt_Out__c,
Number_of_MLs__c,
Number_of_Newsletters_NL__c,
Receipts_Only_No_Other_Mail__c
FROM Contact
WHERE
Id =: cNew.Id
];
//Expected Results:
System.assertEquals('2', savedCon.Recipe__c);
System.assertEquals(true, savedCon.Do_Not_Mail__c);
}