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
vinothvinoth 

Test class on Attachments Trigger.

Hi I need to write test class for the following trigger, but i could able to cover 65%.

 


trigger FormTypeAttachments on Attachment (before insert) {

List<Information_document_scan__c> FormType = [select id, Select_Form_Type__c from Information_document_scan__c where id =:Trigger.New[0].ParentId];
 List<Attachment> atts = Trigger.new;
 for(Attachment a:atts) {
    if(FormType[0].Select_Form_Type__c=='Signature Card'){
        a.addError('Signature Card should have only Four attachments.To go back to record,Click "Done" Button');
    }
  }
}

"a.addError" is not covering, how to Achieve more then 75%?

Navatar_DbSupNavatar_DbSup

Hi,


Try the below code as reference of test method. It will cover 80% of your trigger:
@isTest
private class testTriggerinsert_Contact_ActivityTest
{
public static testMethod void unitTestinsert_Contact_Activity4Task()
{
Information_document_scan__c i=new Information_document_scan__c(name='test',Select_Form_Type__c='Signature Card');
insert i;
Attachment attachment = new Attachment(Name='An attachment',body=blob.valueof('b'),parentId=i.Id);
insert attachment;
}
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

vinothvinoth

Hi Jain,

 

Thanks For ur reply.

 

Still,it cover 65% only. It showing that,

 

" a.addError('Signature Card should have only Four attachments.To go back to record,Click "Done" Button');" 

 

this line is not covered. How to do this? Do I want to write some more code for this "addError" line?

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below modified code:
@isTest
private class testTriggerinsert_Contact_ActivityTest
{
public static testMethod void unitTestinsert_Contact_Activity4Task()
{
Information_document_scan__c i=new Information_document_scan__c(name='test',Select_Form_Type__c='Signature Card');
insert i;
Attachment attachment = new Attachment(Name='An attachment',body=blob.valueof('b'),parentId=i.Id);
try{
insert attachment;
}
catch(exception e)
{}
}
}