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
Chris EdwardsChris Edwards 

Delete within test method is not asserting

Hi all. My first question over here in the foreign climes of the dev boards - go easy on me, I am but a mere admin. :-)

I have written a trigger which stops an account being deleted if one of its field values is not null. This works as expected. I have written the test class, which is at 100% coverage but one of my asserts is failing. The asserts around the delete being blocked work fine - it is the negative case, where the field is null and so where the delete should go ahead, that is not working.

Here is my test class, with the delete and failing assert highlighted:
 
@IsTest
public class PreventAccountDeletionTest {

    private static testMethod void testDeleteSuccess()
    {
           Account acc=new Account(Name='Test Account');
           insert acc;
           
           System.assertNotEquals(acc.Id, null);
           System.assertEquals(acc.Some_Field__c, null);

           delete acc;
                 
           System.assertEquals(acc.IsDeleted, true);
        }

    private static testMethod void testDeleteFail()
    {
           Account acc=new Account(Name='Test Account', Some_Field__c='ABCDEFGHIJKLMNOP');
           insert acc;
  
           try
           {
              delete acc;
              // should throw an exception - the following assertion will cause an error if the code carries on
              System.assert(false);
           }
           catch (DMLException e)
           {
               // expected
               System.assert(e.getMessage().contains('You cannot delete an Account which is linked to the external system.'));
           }
        
        System.assertEquals(acc.IsDeleted, false);
        
    }

}

Any suggestions? Is there something I don't know about deleting in a test class? As the record is (hopefully) in the recycle bin do I need to use something like the equivalent of ALL ROWS? Or any other obvious thing that I'm missing?

Oh and let me know if you want to see the trigger code. Many thanks in advance!
Best Answer chosen by Chris Edwards
BritishBoyinDCBritishBoyinDC
I think you would need to re-query the record to check the IsDeleted flag - if I add something like this to the test, it works:
Account tConfirm = [Select Id, IsDeleted from Account WHERE Id = :acc.Id ALL ROWS];
System.assertEquals(tConfirm.IsDeleted, true);

 

All Answers

Chris EdwardsChris Edwards
Ah, OK, the highlighting didn't work great there. It's the lines between the <b> tags. 
BritishBoyinDCBritishBoyinDC
I think you would need to re-query the record to check the IsDeleted flag - if I add something like this to the test, it works:
Account tConfirm = [Select Id, IsDeleted from Account WHERE Id = :acc.Id ALL ROWS];
System.assertEquals(tConfirm.IsDeleted, true);

 
This was selected as the best answer
James LoghryJames Loghry

Alternatively to BrithishBoy's solution, which should work, you could also just do:

System.assertEquals(0,[Select Id From Account].size());

 And verify that the size of the accounts are what you'd expect, since you're only looking at test data.

Chris EdwardsChris Edwards
Heroes! Thank you both for your answers. They both make sense, but I went with the first suggestion from BritishBoy, which is working perfectly.

Thanks guys: I leave here with working code and increased knowledge!