Unit testing a Domain method

The following test method can be found in the ContestantsTest class and demonstrates mocking Selector method responses to return database rows created in memory. It tests that the same records are subsequently passed to a mocked Unit Of Work:

@IsTest 
private static void whenAwardChampionshipPointsUowRegisterDirty() { 
     
    fflib_ApexMocks mocks = new fflib_ApexMocks(); 
     
  // Given 
    fflib_SObjectUnitOfWork mockUow = (fflib_SObjectUnitOfWork)
mocks.factory(fflib_SObjectUnitOfWork.class); Application.UnitOfWork.setMock(mockUow); Id testContestantId =
fflib_IDGenerator.generate(Contestant__c.SObjectType); List<Contestant__c> testContestants = new List<Contestant__c> { new Contestant__c ( Id = testContestantId, RacePosition__c = 1 )}; // When Contestants contestants = new Contestants(testContestants); contestants.awardChampionshipPoints(mockUow); // Then ((fflib_SObjectUnitOfWork) mocks.verify(mockUow, 1)).registerDirty( fflib_Match.sObjectWith( new Map<SObjectField, Object>{ Contestant__c.Id => testContestantId, Contestant__c.RacePosition__c => 1, Contestant__c.ChampionshipPoints__c => 25} )); }

Consider the following notable aspects of the preceding test code:

  • An in-memory list of records is created in the test code and passed to the Domain classes constructor. The awardChampionshipPoins method being unit tested is then called to determine whether it correctly interacts with the Unit Of Work.
  • A custom ApexMocks matcher is used to check that the SObject passed to the mocked fflib_SObjectUnitOfWork.registerDirty method is the expected method. The matcher also helps the test code confirm that the correct championship points have been calculated.
  • As this test uses a mocked Unit Of Work, no DML is performed.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.10.246