I'm having some problems with a test class and I honestly can't figure out why. Maybe I've been staring at it for too long, but in any case - would appreciate if someone can tell me where I've gone wrong / why these particular lines aren't being run.
Background: I've created a without sharing method that should delete events if any user changes the Status__c field to 'Cancelled'. Under certain circumstances, I just want to delete it plain and simple - but in some conditions I am creating a copy of the deleted Event in a custom "Cancellation History", and link it to the appropriate Lead or Account.
In this particular case, I'm only focusing on a test method to run code which deletes records that have NULL for WhatId and WhoId (linked to nothing) - seems pretty simple.
My Test Method:
@isTest static void unlinkedEvent() {
// Implement test code
List<Event> unlinkedEvents = TestDataFactory.createEvents(null,205,true);
List<Event> eventsToUpdate = new List<Event>();
Test.startTest();
if(!unlinkedEvents.isEmpty()){
for(Event ev : unlinkedEvents){
ev.Status__c = 'Cancelled';
ev.Cancellation_Type__c = 'Millennium S.I. Initiated';
ev.Cancellation_Reason__c = 'Booking Error';
eventsToUpdate.add(ev);
}
}
if(!eventsToUpdate.isEmpty()){
update eventsToUpdate;
}
Test.stopTest();
}
TestDataFactory.createEvents:
public static List<Event> createEvents(Id record,Integer numEvents,Boolean shouldInsert) {
String account_prefix = Schema.SObjectType.Account.getKeyPrefix();
String lead_prefix = Schema.SObjectType.Lead.getKeyPrefix();
List<Event> events = new List<Event>();
for(Integer i=0;i<numEvents;i++) {
Event e = new Event(Subject='TestEvent' + i,
Cancellation_Type__c = '',
Cancellation_Reason__c = '',
Status__c = 'Open',
Confirmed_By__c = 'Test User',
Confirmed_On__c = System.now(),
Department__c = 'Education',
Activity__c = 'Training 1',
EndDateTime = System.now(),
StartDateTime = System.now(),
Description = 'Test Description',
Event_Length_In_Minutes__c = '60',
Event_Notes__c = 'Test Notes');
System.debug('Event e :: ' + e + ' Subject: ' + e.Subject + ' Activity: ' + e.Activity__c);
// If record Id is null, use a more generic Event Type (Like an Internal Meeting)
if(record == null){
e.Department__c = 'General';
e.Activity__c = 'Meeting';
}
// If record Id is a Lead, Use Lead Centric Event Types and link WhoId to record
else if(record != null && ((String)record).startsWith(lead_prefix)){
e.WhoId = record;
e.Department__c = 'Sales';
e.Activity__c = 'Demo';
}
// If record Id is an Account, leave event types alone and link record to WhatId
else if(record != null && ((String)record).startsWith(account_prefix)){
e.WhatId = record;
}
System.debug('WhatId: ' + e.WhatId + ' WhoId: ' + e.WhoId);
events.add(e);
}
if(shouldInsert) insert events;
System.debug('Events: ' + events);
return events;
}
My expectation is that this test method should execute the eventsToDelete.add(e); // THIS SHOULD RUN BECAUSE CONDITIONS MET???
line below, based on the fact that I am passing null as the record Id to TestDataFactory.createEvents, which should result in 205 events being created with no assigned WhatId or WhoId:
public static void cancelledEvent(Map<Id,Event> newEventMap,Map<Id,Event> oldEventMap)
{
Map<Id, Event> eventMap = new Map<Id, Event>([select Id, Activity__c,WhatId,WhoId,Confirmation_Status__c,Status__c,Cancellation_Reason__c,Cancellation_Type__c,Confirmed_By__c,Confirmed_On__c,Department__c,StartDateTime,EndDateTime,Event_Length_in_Minutes__c,Description,Event_Notes__c,Location,Subject from event where id in: newEventMap.keySet()]);
List<Event> eventsToDelete = new List<Event>();
List<Cancellation_History__c> cancellations = new List<Cancellation_History__c>();
List<Cancellation_History__c> cancelsToInsert = new List<Cancellation_History__c>();
String account_prefix = Schema.SObjectType.Account.getKeyPrefix();
String lead_prefix = Schema.SObjectType.Lead.getKeyPrefix();
Decimal eventLength = null;
for(event e: eventMap.values()){
if(e.Status__c == 'Cancelled' && oldEventMap.get(e.Id).Status__c != 'Cancelled'){
// If the Event is not Linked to Anything
if(e.WhatId == null && e.WhoId == null){
eventsToDelete.add(e); // // THIS SHOULD RUN BECAUSE CONDITIONS MET???
// Do not build Cancellation History record since it does not apply to any WhatId or WhoId
}
** Rest of Code Omitted for post length purposes **
So, I have a test method that runs successfully (I know it has no asserts and therefore isn't really a test YET, I plan on adding them after I get line coverage requirement), that method runs a
TestDataFactory.createEvents
method and passes a null record Id which should create 205 events without any assigned WhatId or WhoId, and then the test method loops through the list of 205 events to set each one to Cancelled and add it to an update list before finally updating the list after the loop.This method makes it past the
if(e.Status__c == 'Cancelled' && oldEventMap.get(e.Id).Status__c != 'Cancelled'){
line so I presume that I am successful with inserting events and updating them to Cancelled, but the Dev Console still indicates that the commented line (eventsToDelete.add(e);
) wasn't run. This of course should mean that the events WERE linked to a WhatId or WhoId and didn't pass theif(e.WhatId == null && e.WhoId == null){
check, but they shouldn't be because the TestDataFactory method shouldn't assign either of those fields ifrecord == null
- What am I missing?