From b9288bdc6fdf80981a6e943f06bcf8761823c4c4 Mon Sep 17 00:00:00 2001 From: Jake Richter Date: Fri, 28 Apr 2023 20:50:41 -0400 Subject: [PATCH] unit test for registerDirty with relationship to new sObject --- .../classes/fflib_SObjectUnitOfWorkTest.cls | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/sfdx-source/apex-common/test/classes/fflib_SObjectUnitOfWorkTest.cls b/sfdx-source/apex-common/test/classes/fflib_SObjectUnitOfWorkTest.cls index 582e759bc38..57cd6bfe6a5 100644 --- a/sfdx-source/apex-common/test/classes/fflib_SObjectUnitOfWorkTest.cls +++ b/sfdx-source/apex-common/test/classes/fflib_SObjectUnitOfWorkTest.cls @@ -608,6 +608,65 @@ private with sharing class fflib_SObjectUnitOfWorkTest ); } + @IsTest + private static void testRegisterDirtyRelatedToNewSobject() { + // GIVEN an existing opportunity + Opportunity existingOpp = new Opportunity( + Id = fflib_IDGenerator.generate(Schema.Opportunity.SObjectType), + Name = 'Existing Opportunity', + StageName = 'Closed', + CloseDate = System.today() + ); + // AND a new Account to which the existing opportunity will be related to + Account newAccount = new Account( + Name = 'New Account' + ); + + // WHEN + Test.startTest(); + MockDML mockDML = new MockDML(); + List mySobjects = new List{ + Account.SObjectType, + Opportunity.SObjectType + }; + fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(mySobjects, mockDML); + uow.registerNew(newAccount); + uow.registerDirty(existingOpp, Opportunity.AccountId, newAccount); + uow.commitWork(); + Test.stopTest(); + + // THEN + System.assert( + new fflib_MatcherDefinitions.SObjectsWith( + new List>{ + new Map + { + Account.Id => newAccount.Id, + Account.Name => 'New Account' + } + } + ).matches(mockDML.recordsForInsert), + 'The new accoout record does not match' + ); + + // AND + + System.assert( + new fflib_MatcherDefinitions.SObjectsWith( + new List>{ + new Map + { + Opportunity.Id => existingOpp.Id, + Opportunity.Name => 'Existing Opportunity', + Opportunity.StageName => 'Closed', + Opportunity.AccountId => newAccount.Id + } + } + ).matches(mockDML.recordsForUpdate), + 'The opportunity record should be related to the new Account' + ); + } + @IsTest private static void testRegisterUpsert() { Opportunity existingOpp = new Opportunity(