diff --git a/sfdx-source/apex-common/test/classes/fflib_SObjectUnitOfWorkTest.cls b/sfdx-source/apex-common/test/classes/fflib_SObjectUnitOfWorkTest.cls index 582e759bc3..57cd6bfe6a 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(