3

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());
}
}
| improve this question | | | | |
  • 3
    Can you at least include the failure reason verbatim? It should give you an explicit reason. – Adrian Larson Dec 23 '16 at 19:49
  • were you able to find the reason. ? – user2815826 Mar 6 '17 at 11:24
1

Make the changes in code as below . And you would be able to pass the trailhead.

workUnit.registerRelationship(myNote,Note.ParentId, cont); workUnit.registerNew(myNote, Note.ParentId, cont);

Make this as answer if you pass.. :P

| improve this answer | | | | |
2

While it works, you don't need to resgister the relationship twice. Once registered, you may simply RegisterNew the note ( workUnit.registerNew(myNote) )

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.