So I'm trying to write a test class to meet code coverage requirements to push up this trigger:
trigger DeleteEventsOnCancellation on Case (after update) {
for(Case c : system.trigger.new){
if(c.Status == 'Cancelled')
{
List<Event> events = [SELECT ID FROM Event WHERE WhatID = :c.ID];
delete events;
}
}
}
I'm only reaching 50% coverage with this test class and am stuck on how to get that up to 100%. The delete events call is what is being marked as uncovered. Here's the test class:
@isTest
public class testDeleteEventsOnCancellation {
static testMethod void testEventDeletion(){
Profile p = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
User u = new User();
u.FirstName = 'Test';
u.LastName = 'User2';
...
insert u;
User u2 = new User();
u2.FirstName = 'test';
u2.LastName = 'User';
...
insert u2;
Account a = new Account();
a.Name = 'Test Account';
insert a;
Case c = new Case();
c.AccountId = a.Id;
c.Status = 'New';
c.Origin = 'Email';
c.RecordTypeId = Case.SObjectType.getDescribe().getRecordTypeInfosByName().get('Work Order').getRecordTypeId();
insert c;
Event e = new Event();
e.WhatId=c.Id;
e.StartDateTime=datetime.now();
e.EndDateTime=datetime.now();
insert e;
Event e2 = new Event();
e2.WhatId=c.Id;
e2.StartDateTime=datetime.now();
e2.EndDateTime=datetime.now();
insert e2;
test.startTest();
c.Status= '9-Cancelled';
update c;
test.stopTest();
List<Event> events = [SELECT ID,IsDeleted FROM Event WHERE WhatID = :c.ID];
for(Event ev:events){
system.assertequals(ev.IsDeleted,true);
}
}
}
Is there something I need to do differently to cover the delete method?
status
valueCancelled
and that you are setting this value in your test class as9-Cancelled
. Is that correct?