So, UnitOfWork will do inserts first, then updates, then deletes
I would simplify your code to look like this:
if (oldLPP != null {...} // set oldLPP fields
// set newLPP fields
...
// define scope of UoW
Schema.SObjectType[] sobjsList = new Schema.SObjectType[] { Payment__c.SObjectType};
uow = new fflib_SObjectUnitOfWork(sobjsList);
uow.registerNew(newLPP);
// build xrefs
if (oldLPP != null) {
// new -> old
uow.registerRelationship(newLPP,Payment__c.Merged_Into_Payment__c,oldLPP);
// old -> new
uow.registerDirty(oldLPP,Payment__c.Merged_Into_Payment__c,newLPP);
}
uow.commitWork();
There is of course, no rule against explicitly setting the relationship fields yourself
newLPP.Merged_Into_Payment__c = oldLPP.Id;
Your first port of call should be this answer. See also this recent answer
Normally, one needs to inject the mock UnitOfWork into the fflib factories as such:
Application.UnitOfWork.setMock(mockUow);
But in your case, this isn't necessary as the code under test is passed the mockUow directly:
service.insertSerialNumbers(documentItems, assets, mockUow);
Your verify is trying to verify a list of sobjects using the matcher fflib_Match.sObjectsWith
but your code under test calls registerNew
in a loop.
Thus, you need this verify:
((fflib_SObjectUnitOfWork)mocks.verify(mockUow, mocks.times(1).description('Missed serial number insert')))
.registerNew(fflib_Match.sObjectWith( // singular
new Map<SObjectField, Object> {
AST_SerialNumber__c.AST_MaterialDocumentPosition__c => itemId,
AST_SerialNumber__c.Name => relatedAsset.SerialNumber,
AST_SerialNumber__c.AST_CreatorFlag__c => true
}
));
.. and again for each AST_SerialNumber__c your code under test creates
Summary
When verifying a call to the unitOfWork, you need to use the relevant matcher that corresponds to the code under test. If the call to the unitOfWork method has multiple arguments, you need to use matchers for each argument (for example, matching uow.registerRelationship
)
Best Answer
You just shadowed the name of the standard object (
Attachment
) with your instance of it (attachment
). Either rename it:Or if you don't want to change the name of your record instance, you need to use the fully qualified name to disambiguate: