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
Mathew Andresen 5Mathew Andresen 5 

Testing for an update that throws an error

Hi,

I have a tasting object that when saved automatically generates an event.  If that tasting object is updated, it also updates the dates of the event.  Of course if someone deletes the event, that would cause an error on update.  I'm handling the error in the trigger function with an alert about the event.

But I'm not quite sure how to properly test that in my testing class.  I saw in the trailhead example they did something like Database.xxx but I'm not sure how that applies with update, if it even does.

My complete code if it helps
 
trigger TastingEvent on Tasting__c (after insert, after update) {
List<Event> myEvent = new List<Event>();
    User u = [Select id, FirstName from User where id =: UserInfo.getUserId() LIMIT 1];
    
    //Integer size = Trigger.Size;
    if(Trigger.isAfter && Trigger.isInsert && Trigger.size <2)  {
        for(Tasting__c taste:Trigger.New) {
            Event e = new Event();
            String mySubject = (taste.Name + ' - ' + taste.Location__c); // required and default value
            e.Subject = mySubject;
            e.StartDateTime = taste.Date_of_Tasting_Start__c; // required
            e.EndDateTime = taste.Date_of_Tasting_End__c;  // required
            e.WhatId = taste.Id;
            e.OwnerId = u.Id;
            myEvent.add(e);
        }
        insert(myEvent);
        
        
    }
    
    
    if(Trigger.IsAfter && Trigger.isUpdate && Trigger.Size <2) {
        for(Tasting__c taste :Trigger.New) {
            try {
           Event eve = [SELECT Id, StartDateTime, EndDateTime, WhatId FROM Event WHERE WhatID = :taste.Id LIMIT 1];
            eve.StartDateTime = taste.Date_of_Tasting_Start__c;
            eve.EndDateTime = taste.Date_of_Tasting_End__c;
            update(eve);
            } catch(Exception e) {
               Trigger.newMap.get(taste.Id).addError('No matching event to update');
            }
     	}
     }
    
    
}

and the testing class
 
@isTest
Private class TestTastingTrigger {
    @isTest static void TestTasting() {
        // insert feature
      Account newAccount = new Account (Name = 'test Account');
        insert (newAccount);
        String acctId = newAccount.Id;
        
        Test.startTest();
        
       User u = [Select id, FirstName from User where id =: UserInfo.getUserId() LIMIT 1];
       Tasting__c taste = new Tasting__c (Name='Test', 
       	Date_of_Tasting_Start__c = System.Now(),
        Date_of_Tasting_End__c = System.Now(),
        Account__C = acctId );
        insert(taste);

        
        //Perform test
        
        
        Event myEvent = [SELECT Id, WhatId FROM Event WHERE WhatId =:taste.Id];
       	System.assertEquals(taste.Id, myEvent.WhatId);
       
        
        /////////////////////////////////////////////
        // test update feature
        
        DateTime newStart = taste.Date_of_Tasting_Start__c;
        newStart = newStart.addDays(1);
        DateTime newEnd = taste.Date_of_Tasting_End__c;
         newEnd = newEnd.addDays(1);
        taste.Date_of_Tasting_Start__c = newStart;
        taste.Date_of_Tasting_End__c = newEnd;
        
        
       
        update(taste);
        myEvent = [SELECT Id, WhatId, StartDateTime, EndDateTime FROM Event WHERE WhatId =:taste.Id];
        System.assertEquals(taste.Date_of_Tasting_Start__c, myEvent.StartDateTime);
         System.assertEquals(taste.Date_of_Tasting_End__c, myEvent.EndDateTime);
        
        
        delete (myEvent);
        
        // I'm sure this is cheating
        try { update(taste); }
        catch (exception e) {}
                        
    }
    
  
}

Thanks,
Best Answer chosen by Mathew Andresen 5
logontokartiklogontokartik
HI Mathew,

Your test class looks good and no its not cheating, you basically have simulated what user might do, by deleting the event and updating the taste record, which makes perfect sense.

Line 44, when you delete an event and then update the tasting, it catches the error and adds error on record, which is fine, 

BTW, on the other note, the trigger code you have written needs to be revisited, there is a SOQL query and DML updates in the for loop which is not a best practice as it would cause issues when you are updating or inserting more than 200 records at same time

 

All Answers

logontokartiklogontokartik
HI Mathew,

Your test class looks good and no its not cheating, you basically have simulated what user might do, by deleting the event and updating the taste record, which makes perfect sense.

Line 44, when you delete an event and then update the tasting, it catches the error and adds error on record, which is fine, 

BTW, on the other note, the trigger code you have written needs to be revisited, there is a SOQL query and DML updates in the for loop which is not a best practice as it would cause issues when you are updating or inserting more than 200 records at same time

 
This was selected as the best answer
Mathew Andresen 5Mathew Andresen 5
Thanks for the reply.

As it regards to the for loop, I put a if statement in so the for loop only triggers if the trigger.new <2  so bulk updates shouldn't trigger at all right?  When I made another test class and put tried to put multiple records in, I didn't see any events get created.

I wasn't sure how to test that in code, so just did it manually.