+ Start a Discussion
viswadaviswada 

Test Coverage for meassaging Mails

HI have one trigger  sending mail , i need to test coverqage for that tigger , My trigger

 

is

trigger sendemail on Anxiety_Survey_del__c (before insert,before update) {
for(Anxiety_Survey_del__c ase:trigger.new){
if(trigger.isinsert){
Integer cou=0;
cou=[select count() from Anxiety_Survey_del__c where Patient__c=:ase.Patient__c and Surevey_Number__c>:0 limit 4];
If(ase.Anxiety_Date_and_Time__c!=null)
ase.X30_days_mail__c=date.NewInstance(ase.Anxiety_Date_and_Time__c.year(),ase.Anxiety_Date_and_Time__c.month(),ase.Anxiety_Date_and_Time__c.day()).adddays(30);
If(cou<4)
ase.Surevey_Number__c=cou+1;
}
if(trigger.isupdate){
If(ase.Survey_Created__c==false){
Integer cou=[select count()from Anxiety_Survey_del__c where Patient__c=:ase.Patient__c and Surevey_Number__c>:ase.Surevey_Number__c limit 1];
If(cou==0)
{
list<feeditem> posts=new list<feeditem>();
list<Send_Email__c> emails=new list<Send_Email__c>();

if((ase.Temp__c=='Temp172'&&Trigger.oldmap.get(ase.id).Temp__c!='Temp172')||(ase.Send_Daily_email__c!=null)){
string days;
 If(ase.Surevey_Number__c>0&&ase.Surevey_Number__c<4)   
                    days=ase.Surevey_Number__c*30 +' days';
                    patient__c p1=[select id,Name,ownerid,Last_name__c from patient__c where id=:ase.patient__c limit 1];
                   User p=[select id,email from user where id=:p1.ownerId limit 1];
                   If(p!=null){
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    String[] toAddresses =new string[]{p.email};
                    mail.setToAddresses(toAddresses);
                    string sub='This is time for Another Survey for Patient '+p1.Name+' '+p1.Last_name__c ;
                    mail.setSubject(sub);
                    mail.setBccSender(false);
                    mail.setUseSignature(false);                     
                   
                    mail.plaintextbody='This is time for '+days +' Survey for Patient '+p1.Name+' '+p1.Last_name__c ;              
                    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                    }
                    //System.debug('$$$$$'+p1.ownerId);
                    FeedItem post = new FeedItem();
                    post.ParentId = p.Id;
                    post.Body = 'This is time for '+days +' Survey for Patient '+p1.Name+' '+p1.Last_name__c ; // And i was... super simple :D
                    posts.add(post);
                    If(ase.Temp__c=='Temp172'&&Trigger.oldmap.get(ase.id).Temp__c!='Temp172'){
                    Send_Email__c email1=new Send_Email__c();
                    email1.Date_to_send__c=ase.X30_days_mail__c.adddays(1);
                    email1.Anxiety_Survey__c=ase.id;
                    emails.add(email1);
                    }
                    
                    
}
if(posts.size()>0)
insert posts;
If(emails.size()>0)
insert emails;
}
else
ase.Survey_Created__c=true;
}
}
}
}

 

 

I wrote test test class for the trigger bu t i don;t know how to  write text  fo r senfin mails plz help me

 

 

 

@isTest
Private class Testsendemail
{
private static TestMethod void Testsend1()
{
  Patient__c  p =new Patient__c(E_LastName__c='xxxx',E_Electronic_Chart_ID__c='aaaaaa');
 insert p;
 Patient__c  p1 =new Patient__c(E_LastName__c='yyyyy',E_Electronic_Chart_ID__c='zzzzz');
 insert p1;
 Anxiety_Survey_del__c  a=new Anxiety_Survey_del__c(Name='aaaaa',Patient__c =p.id,Survey_Created__c=false,Surevey_Number__c=3,Anxiety_Date_and_Time__c=system.today(),Temp__c='aaaa');
 insert a;
 Anxiety_Survey_del__c  a1=new Anxiety_Survey_del__c(Name='bbbb',Patient__c = p.id,Survey_Created__c=false,Surevey_Number__c=3, Anxiety_Date_and_Time__c=system.today(),Temp__c='bbbbb');
 //insert a1;
  }
  }

 

mohimohi

1-Please make a check on your condition 

2-You need to create user then run as user then create your Record,please test whether your record meeteing 

 If(ase.Surevey_Number__c>0&&ase.Surevey_Number__c<4)    this condition then it automatic execute your scenario.