Skip to main content
Hi

I am trying to write a test class for a trigger. The trigger is on attachement object , basically the trigger has a buch of .addError statements based on some fields of parent object of attachement which is to prevent user from updating, deleting or inserting attachements to that object based on a piclist field of that object. But i am strugling here with the test class, Only 65% coverage so far.

    How should I create test data for attachment for testing purpose and I need to test for all the differnt scenarios, insert, update and delete.

Please help mw eith some examples, I can post my trigger if required.

Thank you
10 respuestas
  1. 9 sept 2016, 2:20
    Update Method would be something llike, please note that i dont have the structure of your custom object so you will have to create that record first

     

    static testMethod void testUpdateAttachment()

    {

    Test.startTest();

    //Custom object record insert

    Blob b = Blob.valueOf('test data');

    Attachment attachment = new Attachment();

    //objectid is id of the record in which you want to create attachment, it could be a test account etc attachment.ParentId = objectId;

    attachment.Name = 'Test Attachment for Parent';

    attachment.Body = b;  

    insert(attachment);

    Attachment at = [Select Id, Body, Name, ParentId from Attachment where ParentId =: objectId];

    Blob b2 = Blob.valueOf('test 2 data');

    at.body = b2;

    update b2;

    Test.stopTest();

    }

    Hope this helps

     
0/9000