+ Start a Discussion
DrewK.ax1106DrewK.ax1106 

Test class help for custom object

Very new to Apex code, created the following class but I am having a miserable time creating a correct test class for it. Would anyone be able to help me out so I can go back over it and figure it out? Important to note I have already read multiple articles on test classes, just unable to master it.  Thanks.

 

 

 

 

 1  public with sharing class DisplaySectionsController {
 2   public List<Patient_Drugs__c> ptdrugs {get;set;}
 3   public String messages_equi {get;set;}
 4   public void load() {
 5   ptdrugs= [Select Name,Patient__c,Prescriber__c,Patient__r.Notes_from_HC__c,Drug__r.Name,Prescriber__r.Name,Patient__r.Patient_Health_Center__r.Name,Patient__r.Name,Patient__r.Date_of_Birth__c,Patient__r.Center_EMR_Patient_ID_Pt_Act__c,Patient__r.Residence_Phone__c From Patient_Drugs__c WHERE Patient__c=:ApexPages.currentPage().getParameters().get('id') and Rx_Request_Send_To__c = 'Prescriber' AND Rx_Received_Date__c=null order by Prescriber__c];
 6   if(ptdrugs.Size()==0)
 7   {
 8   messages_equi='EQ';
 9  
 10  
 11   }
 12   }
 13   public List<SelectOption> getP() {
 14   Map<String,selectOption> third1 = new Map<String,selectOption>();
 15   List<SelectOption> options= new List<SelectOption>();
 16  
 17   for (Patient_Drugs__c pd : [SELECT Prescriber__c,Prescriber__r.Name FROM Patient_Drugs__c WHERE Patient__c=:ApexPages.currentPage().getParameters().get('id') and Prescriber__r.Name<>'' and Rx_Request_Send_To__c = 'Prescriber' AND Rx_Received_Date__c=null order by Prescriber__r.Name])
 18   {
 19  
 20   if(pd.Prescriber__r.Name!= null)
 21   {
 22  
 23   third1.put('',new selectOption('', '-- None --'));
 24   third1.put(pd.Prescriber__r.Name,new selectOption(pd.Prescriber__c,pd.Prescriber__r.Name));
 25  
 26   }
 27  
 28   }
 29  
 30   options= third1.values();
 31   return options;
 32   }
 33  
 34  
 35  }
Best Answer chosen by Admin (Salesforce Developers) 
SRajesh8SRajesh8

Hi

 

 

Try this

 

@isTest

private class DisplaySectionsController_test{

public static testmethod void DisplaySectionsController_test()

{

Patient_Drugs__c pd = new Patient_Drugs__c();

pd.Name='tset';

pd.Patient__c='rtre';

.

.

.

 

//like this insert the values which are in query and insert mandatory fields in   Patient_Drugs__c

DisplaySectionsController  dsc = new DisplaySectionsController();

dsc.load();

 

insert the values  again for Patient_Drugs__c in a query ain this according to if condition i.e Prescriber__r.Name!= null

List<selectoption> so = new List<selectoption>()

so=dsc.getP();

 

 

 

}}

 

Regards,

Rajesh.

All Answers

SRajesh8SRajesh8

Hi

 

 

Try this

 

@isTest

private class DisplaySectionsController_test{

public static testmethod void DisplaySectionsController_test()

{

Patient_Drugs__c pd = new Patient_Drugs__c();

pd.Name='tset';

pd.Patient__c='rtre';

.

.

.

 

//like this insert the values which are in query and insert mandatory fields in   Patient_Drugs__c

DisplaySectionsController  dsc = new DisplaySectionsController();

dsc.load();

 

insert the values  again for Patient_Drugs__c in a query ain this according to if condition i.e Prescriber__r.Name!= null

List<selectoption> so = new List<selectoption>()

so=dsc.getP();

 

 

 

}}

 

Regards,

Rajesh.

This was selected as the best answer
DrewK.ax1106DrewK.ax1106

Awesome, 80% coverage. Reviewed the code and it is starting to make sense. Thanks!