Skip to content

Commit

Permalink
unit test for registerDirty with relationship to new sObject
Browse files Browse the repository at this point in the history
  • Loading branch information
jprichter committed Apr 29, 2023
1 parent 5bc74ad commit b9288bd
Showing 1 changed file with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Schema.SObjectType> mySobjects = new List<Schema.SObjectType>{
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<Map<SObjectField, Object>>{
new Map<SObjectField, Object>
{
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<Map<SObjectField, Object>>{
new Map<SObjectField, Object>
{
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(
Expand Down

0 comments on commit b9288bd

Please sign in to comment.