I am not able to understand the following behavior:
- New list to be inserted in an apex class using fflib/UoW
uow.registerNew(myList)
This is the output using System.debug(JSON.serialize(myList));
WorkingHours__c:{End__c=2021-11-22 15:00:00, WorkShift__c=a1R000000000004EAA, Start__c=2021-11-22 09:00:00, WorkPlan__c=a1Q000000000003EAA, User__c=005000000000001AAA}
- Mocked list created using sfab_FabricatedSObject to test the registerNew and added to the mocks.verify
((fflib_ISObjectUnitOfWork) mocks.verify(unitOfWorkMock)).registerNew(myMockedList);
This is the output using System.debug(JSON.serialize(myMockedList)):
WorkingHours__c:{Start__c=2021-11-22 09:00:00, End__c=2021-11-22 15:00:00, User__c=005000000000001AAA, WorkShift__c=a1R000000000004EAA, WorkPlan__c=a1Q000000000003EAA}
Finally mocks.verify is throwing an exception because both lists are not equals. As a double check, comparing both lists using List1.equals(List2) again is returning false.
What's the reason?
EDIT:
Trying to debug the issue, I identify that removing Start__c and End__c (both datetime fields) from both lists the problem doesnt exist. mocks.verify and List1.equals(List2) are returning true
As a workaround, instead of comparing directly both lists, I am going to use the fflib_Match.sObjectsWith(toMatch) comparing one by one all the values, in this case again is working perfectly:
for(WorkingHours__c wh : workingHours){
toMatch.add(new Map<SObjectField, Object>{
WorkingHours__c.WorkShift__c => wh.WorkShift__c,
WorkingHours__c.WorkPlan__c => wh.WorkPlan__c,
WorkingHours__c.User__c => wh.User__c,
WorkingHours__c.Start__c => wh.Start__c,
WorkingHours__c.End__c => wh.End__c
});
}
((fflib_ISObjectUnitOfWork) mocks.verify(unitOfWorkMock)).registerNew(fflib_Match.sObjectsWith(toMatch));```
fflib_Match.sObjectWith
is the preferred solution and to use a UtilDate class / property that you can dependency inject a fixed today/now in testmethods gets around your start and end date issue