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
FrescoFresco 

need to cover attachmnets in test class

class:
------
In my class attachments are like below
List<Attachment> kycAtt=[select id,name,body,BodyLength from Attachment where parentId=:kyc.Id and BodyLength>0];
              List<Attachment> erpAtt=[select id,name,body,BodyLength from Attachment where parentId=:erp.Id and BodyLength>0];

 Messaging.EmailFileAttachment[] efaList=new Messaging.EmailFileAttachment[] {};
              
              if(kycAtt.size()>0){
                  for(Attachment att:kycAtt){
                      Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
                      efa.setFileName(att.name);
                      efa.setBody(att.body);
                      efaList.add(efa);
                  }
              }
              
              if(erpAtt.size()>0){
                  for(Attachment att:erpAtt){
                      Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
                      efa.setFileName(att.name);
                      efa.setBody(att.body);
                      efaList.add(efa);
                  }
              }
              
              if(efaList.size()>0){
                  mail.setFileAttachments(efaList);
              }

I need to cover these code in test line can you plese help me out
surasura
Hi 

I cant provide the exact code for your problem becuase I do not no  what type of objects you are referring to by varaibles kyc and erp , but what you have to do is quite simple,

1. Define a test class and test method
2 in your test method insert two records of kyc and erp 
3. then create  two attachment records linking to above records via parent id
4. insert those attachment records
5 call your method from test class 

refer below code for creating attachment in test method.
 
Attachment attach = new Attachment();

 attach.Name='Unit Test Attachment';
 Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
 attach.body=bodyBlob;
  attach.parentId= kyc.Id // this should be the id of kyc or erp records your create in test method
   insert attach;

 
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest
private class TestAttachment 
{
	static testMethod void testAttachments()
    {
    	kyc__C kyc=new kyc__C(); // Please add your object KYC heere
        insert kyc;
 
    	Erp__C erp=new Erp__C(); // Please add your erp object here
        insert erp;
 
        Attachment attach=new Attachment();   	
    	attach.Name='Unit Test Attachment';
    	Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
    	attach.body=bodyBlob;
        attach.parentId=kyc.id;
        insert attach;

        Attachment attach1=new Attachment();   	
    	attach1.Name='Unit Test Attachment';
    	Blob bodyBlob1=Blob.valueOf('Unit Test Attachment Body');
    	attach1.body=bodyBlob1;
        attach1.parentId=erp.id;
        insert attach1;
    	
    }
}

 
FrescoFresco
Hi manoj i added but its not covering can you plese give me your mail plese so that i will mail u.