function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MedhanieHabteMedhanieHabte 

Stuck on Unit of Work Principles test in Trailhead

Hi All, I am working on the Unit of Work Principles trailhead and seem to be stuck, I cannot get it to pass even though the test passes. I surmise it may be because it is testing other tests and looking for 100% code coverage across the board. Are there any steps I should take. Here is my code below. Hope it helps.
 
@isTest
public class UnitOfWorkTest {
    @isTest static void challengeComplete(){
        fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(
            new Schema.SObjectType[]{
                Account.SObjectType,
                Contact.SObjectType,
                Note.SObjectType
            }
        );
        
        for (Integer i=0 ; i<100 ; i++) {
            Account a = new Account(Name= 'Test' + i);
            uow.registerNew(a);
            
            for (Integer j=0 ; j<5 ; j++) {
                Contact c = new Contact(LastName = 'Test'+i + ' ' +j);
                uow.registerNew(c, Contact.AccountId, a);
                
                Note n = new Note(Body='Test '+i + '' + j, Title='Test'+i+j);
                //uow.registerRelationship(n, Note.ParentId, a);
                //uow.registerNew(n, Note.ParentId, a);
                uow.registerNew(n, Note.ParentId, c);
            }
        }

        uow.commitWork();
 
        fflib_SObjectUnitOfWork uow2 = new fflib_SObjectUnitOfWork(
            new Schema.SObjectType[]{
                Account.SObjectType,
                Contact.SObjectType,
                Note.SObjectType
            }
        );        
        
        Id oldAccountId;
        Account a2;
        for (Contact c : [SELECT Id, LastName, AccountId, Account.Name, (SELECT Id, ParentId, Title, Body FROM Notes) FROM Contact Order By AccountId, Id]) {
            
            if (oldAccountId != c.AccountId) {
                oldAccountId = c.AccountId;
                a2 = new Account(Id=c.AccountId, Name='Test');
                uow2.registerDirty(a2);
            }
            
                c.LastName = 'Test';
                uow2.registerDirty(c);
                

                c.Notes[0].Body = 'Test';

                uow2.registerDirty(c.Notes[0]);

        }        
        
        test.startTest();
        uow2.commitWork();
        //uow.commitWork();
        test.stopTest();
        
        System.assertEquals(100, [Select Id from Account].size());
        System.assertEquals(500, [Select Id from Contact].size());
        System.assertEquals(500, [Select Id from Note].size());
    }
}

 
Suley KaboreSuley Kabore
Hello! I guess you did not catche the mistake that it should be 'J < 500' and not 'J <5' And I do not think that the Conatc objects are supposed to be related to the Account object. Here is a working code. I hope that it helps.

@isTest
public class UnitOfWorkTest {
    @isTest
    static void challangeComplete()
    {
        fflib_sobjectUnitOfWork workUnit = new fflib_sobjectUnitOfWork(new Schema.SObjectType[]{Account.sObjectType, Contact.sObjectType, Note.sObjectType}); 
       for(Integer i= 1; i <= 100; i++)
       {
           Account acc = new Account();
           acc.Name = 'Fanancial'+i;
           workUnit.registerNew(acc);
       }
        
        for(Integer i = 1; i <= 500; i++)
        {
            Contact cont = new Contact();
            cont.LastName = 'LePodium' + i;
            workUnit.registerNew(cont);
            
            Note myNote = new Note();
            myNote.Title = 'Test' + i;
            
            workUnit.registerNew(myNote, Note.ParentId, cont);
           // workUnit.registerNew(myNote);
        }
        
        test.startTest();
        workUnit.commitWork();
        test.stopTest();
        
        system.assertEquals(100, [SELECT Id FROM Account].size());
        system.assertEquals(500, [SELECT Id FROM Contact].size());
        system.assertEquals(500, [SELECT Id FROM Note].size());
    }

}
Jess BurghJess Burgh
Hey all. Having a bit of trouble with this challenge. Any code I begin to write I get a long list of errors. User-added image

Any suggestions would be appreciated. Thanks!!