You need to sign in to do that
Don't have an account?
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()); } }
@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());
}
}
Any suggestions would be appreciated. Thanks!!