In my controller i'm using connectApi and my class functionality is working fine but the issue is when i'm using in my Test class (SeeAllData=true) it's covered 100% but seeAllData = true is not considering in best practice what the alternate to avoid seeAllData true.
without seeAllData true its throw an error System.UnsupportedOperationException: ConnectApi methods are not supported in data siloed tests. Please use @IsTest(SeeAllData=true).
Best Answer
The alternative to using
seeAllData=true
is applying a mocking or delegate pattern so that the Connect API is not called in test context, but you can still validate behavior. Here's a simple and very explicit example (noStubProvider
):Then, in test context, we have the freedom to inject a completely different class that implements
PostsToChatter.ChatterPoster
, but doesn't really call the Connect API. Meanwhile, we've kept our actual-Chatter delegate so tiny and simple that we can still call it in test context to get the code coverage, even though we can't validate its behavior like we ought to.