[SalesForce] System.AssertException: Assertion Failed: Expected: 2, Actual: 3

Earlier this is working with 3 but due to few modifications to this class, Now it is not working. What would be the problem if i change like

System.assertEquals(lAccrSaleConShare.size(), 2);

Best Answer

Don't follow the example set by this code.

  • The syntax is system.assertEquals(expected_value, actual_value, message).
    • In the assertions above, expected_value and actual_value are flipped.
    • While the message argument is optional, it is highly recommended and might have helped you here.
  • When using magic numbers in your unit tests, you can give them more meaning by making them constants. Most of my test classes have a line static final Integer RECORD_COUNT = Limits.getLimitQueries() + 1, which you could use in place of 3.
  • You aren't even testing the setManualShareAfterInsert method. You're testing a trigger somewhere, which you haven't shared with us.
  • Same goes for the setManualShareAfterUpdate method.

I can't tell you how to get your unit test working. I can't even tell if your code should work as advertised. But you might have some luck by working on Separation Of Concerns. You could break out a few discrete tasks and verify they work as expected, then build on that. The key to unit testing is to test small units of code.

I have found trying to build on top of code that tries to do dozens of things in one method is just not tenable. Here's a start for how I would refactor:

public static Set<Id> pluckIds(String field, List<SObject> records)
{
    Set<Id> ids = new Set<Id>();
    for (SObject record : records)
        ids.add((Id)record.get(field));
    ids.remove(null);
    return ids;
}
public static Map<Id, Accreditation__c> getAccreditations(List<Accreditation_Sales_Contact__c>  records)
{
    return new Map<Id, Accreditation>([
        SELECT Id, Name, Account__r.Partner_Community_User_Role_ID__c
        FROM Accreditation__c
        WHERE Id IN :pluckIds('Accreditation__c', records)
    ]);
    // if you query the Account__r.Partner_Community_User_Role_ID__c field,
    // you don't even need to query Account at all, just reference this way
}

Anyway, that's probably not the answer you were looking for, but hopefully it will help you to some extent.

Related Topic