You need to sign in to do that
Don't have an account?
Chris 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:
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!
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!
All Answers
Alternatively to BrithishBoy's solution, which should work, you could also just do:
And verify that the size of the accounts are what you'd expect, since you're only looking at test data.
Thanks guys: I leave here with working code and increased knowledge!