A developer has a test class that creates test data before making a mock callout but now receives a 'You have uncommitted work pending. Please commit or rollback before calling out' error.
Which step should be taken to resolve the error?
When working with Apex tests that involve callouts, it's important to avoid the 'uncommitted work pending' error. This error typically occurs because DML operations (like inserts) are pending when a callout is attempted. Salesforce requires that all callouts are made before any DML operations in a transaction. To avoid this, the pattern is to insert the records before calling Test.startTest(), and then make the callout after this call. Test.startTest() marks the point where your actual test begins, and it resets the governor limits. By making the callout after this point, it ensures that the DML operations are committed and you are within a new execution context regarding governor limits.
Testing HTTP Callouts by Implementing the HttpCalloutMock Interface
Understanding Execution Contexts
Stephen
3 days agoCarey
8 days agoMichell
13 days agoErinn
18 days agoMaryrose
24 days agoBrittney
29 days agoTelma
1 month agoShala
1 month agoBeth
1 month agoBarrie
2 months agoStephanie
2 months agoGilma
2 months agoLai
2 months agoCathrine
2 months agoOrville
3 months agoAntonio
3 months agoVeronika
3 months agoCandida
3 months agoBelen
3 months ago