We're big fans of doing unit tests using as little DML as possible, mocking SObjects using various techniques
Here's a simple example
public class FoosService {
public void doBar(Set<Id> ids) {
... do work ...
... oops ... throw BarException
}
}
public class FoosDomain {
public void onAfterInsert(SObject[] records) {
try {
FooService.doBar(new Map<Id,SObject>(records).keySet();
}
catch (FoosService.BarException e) {
// apply addError(e) to relevant rows in records
}
}
}
In the unit testmethod:
Foo__c[] mockFoos = new List<Foo__c> {
new Foo__c(...),
new Foo__c(...) // causes exception in FoosService.doBar
};
new FoosDomain.onAfterInsert(mockFoos);
you want to be able to do this:
System.assertEquals(true,mockFoos[1].hasError(),'sb marked as error');
but there is no SObject method to see if a given SObject has had addError(...)
applied to it.
Known workarounds:
fflib-apex-common fflib_SObjectDomain class which supports this testable construction:
someSobject.addError(error('message',someSobject));
System.assertEquals('message',
fflib_SObjectDomain.Errors.getAll()[1].message);
and as we're an fflib shop, I'm happy to use this
Is there some other "trick" I don't know about to discover in an assert that a non-DML'd SObject has had
addError
applied?