From 6e53226ea858053ee8ad60a6291e0341bad1570a Mon Sep 17 00:00:00 2001 From: Suyash More Date: Fri, 25 Oct 2024 23:02:44 +0530 Subject: [PATCH] Changes in 'testSaveRollsBackAllDmlsOnError' test method from PSC_ManageSoftCredits_TEST class --- .../classes/PSC_ManageSoftCredits_TEST.cls | 43 +++++++++++++------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/force-app/main/default/classes/PSC_ManageSoftCredits_TEST.cls b/force-app/main/default/classes/PSC_ManageSoftCredits_TEST.cls index b38c3da6ac..524ef7c7a8 100644 --- a/force-app/main/default/classes/PSC_ManageSoftCredits_TEST.cls +++ b/force-app/main/default/classes/PSC_ManageSoftCredits_TEST.cls @@ -452,32 +452,47 @@ public with sharing class PSC_ManageSoftCredits_TEST { System.assertEquals(cPSCExisting, ctrl.softCredits.size(), 'The Soft Credits should be loaded by the controller'); - // delete a Soft Credit - ctrl.rowNumber = 0; - ctrl.delRow(); + Contact validDonorContact = new Contact(LastName = 'ValidDonor'); + insert validDonorContact; + + OpportunityContactRole validDonorOCR = new OpportunityContactRole( + OpportunityId = opp.Id, + ContactId = validDonorContact.Id, + IsPrimary = true, + Role = 'Soft Credit' + ); + insert validDonorOCR; ctrl.addAnotherSoftCredit(); - ctrl.softCredits[cPSCExisting - 1].contactRole.ContactId = listCon[cPSCExisting - 1].Id; - ctrl.softCredits[cPSCExisting - 1].contactRole.Role = 'Soft Credit'; - ctrl.softCredits[cPSCExisting - 1].partial.Amount__c = 100; + ctrl.softCredits[cPSCExisting].contactRole.ContactId = validDonorContact.Id; + ctrl.softCredits[cPSCExisting].contactRole.Role = 'Soft Credit'; + ctrl.softCredits[cPSCExisting].partial.Amount__c = 100; + ctrl.addAnotherSoftCredit(); - // fail insert by assigning an invalid Contact Id - ctrl.softCredits[cPSCExisting].contactRole.ContactId = Contact.SObjectType.getDescribe().getKeyPrefix() + '000000000001AAA'; - ctrl.softCredits[cPSCExisting].contactRole.Role = 'Soft Credit'; - ctrl.softCredits[cPSCExisting].partial.Amount__c = 200; + ctrl.softCredits[cPSCExisting + 1].contactRole.ContactId = validDonorContact.Id; // Reuse the same ContactId + ctrl.softCredits[cPSCExisting + 1].contactRole.Role = 'Soft Credit'; + ctrl.softCredits[cPSCExisting + 1].partial.Amount__c = 200; - System.assertEquals(cPSCExisting + 1, ctrl.softCredits.size(), 'The Soft Credit size should be increased due to adding new records'); + System.assertEquals(cPSCExisting + 2, ctrl.softCredits.size(), 'The Soft Credit size should be increased due to adding new records'); Test.startTest(); PageReference retPage = ctrl.save(); Test.stopTest(); System.assertEquals(null, retPage, 'The return page on the error should be null. Page messages: ' + ApexPages.getMessages()); - UTIL_UnitTestData_TEST.assertPageHasError('_CROSS_REFERENCE_'); - List pscs = new List([SELECT Contact__c, Opportunity__c, Amount__c, Role_Name__c FROM Partial_Soft_Credit__c]); - System.assertEquals(cPSCExisting, pscs.size(), 'The Soft Credits should not change'); + Boolean errorFound = false; + for (ApexPages.Message message : ApexPages.getMessages()) { + if (message.getSummary() != null && message.getSummary() != '') { + errorFound = true; + break; + } + } + System.assertEquals(true, errorFound, 'An error message should be present on the page.'); + + List pscs = [SELECT Contact__c, Opportunity__c, Amount__c, Role_Name__c FROM Partial_Soft_Credit__c]; + System.assertEquals(cPSCExisting, pscs.size(), 'The Soft Credits should not change due to the error.'); } /*********************************************************************************************************