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
pjaenickepjaenicke 

Trouble with AssertEquals

Greetings,

 

I'm pulling my hair out trying to find out why my test method is failing.

 

I've "borrowed liberally" from Colin Loretz's page on creating rollup fields between any object, (x-ref http://colinloretz.com/tag/salesforce-apex-code/).

 

The trigger I've created aggregates values from "child" records, placing the total value on the parent.  Works great.

 

The test method however, fails on the assertequals clause :

 

...

    // Test ParentObj__c "p" created, which automatically spawns the

    // creation of a default first ChildObj__c with a Percent__c equal to 100.

...

    List<ChildObj__c> ChildObjList = new List<ChildObj__c>();
    ChildObj__c c1 = new ChildObj__c(ParentObj__c=p.id,Percent__c=10);
    ChildObj__c c2 = new ChildObj__c(ParentObj__c=p.id,Percent__c=25);
    ChildObj__c c3 = new ChildObj__c(ParentObj__c=p.id,Percent__c=50);

    ChildObjList.add(c1);
    ChildObjList.add(c2);
    ChildObjList.add(c3);

    //Insertion test 100 + 10 + 25 + 50 = 185
    {
        insert ChildObjList;
        p = [select id, Total_Percent__c from ParentObj__c where id=:p.Id];
        System.assertEquals(185,Parent.Total_Percent__c);

        // "System.AssertException: Assertion Failed: Expected: 185, Actual: 555" ?!?!?!

    }

...

 

If I set my "expected" value to 555, then continue with update and deletion tests, things seem to normalize.  I noticed that the "Actual Result" is always three times the expected, (because I'm inserting three records).  If I only do two inserts, then the delta is off by two times the expected, (and I can avoid the issue entirely if I do a single record test, but I'd really like to understand why this is happening).

 

Thanks for any insight,

Pete

 

nickname142857nickname142857

Suggestion:

1) Add System.assert(100, p.TotalPercent__c); before inserting the children.

2) If that is fine, then select all the children objects after inserting them and display (or assert their Percent__c).

 

pjaenickepjaenicke

Both of those asserts worked fine :/

 

In the meantime, I've eliminated the 2nd and 3rd entries, (although including a bulk test method at the end).