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
Brian Cherry FWIBrian Cherry FWI 

First Controller

My first controller, can anyone help creating a test class for this?

public class deliverablesController {
  
  // Constructor
 public deliverablesController(ApexPages.StandardController controller) {
  this.proj = (Professional_Services_Partner__c)controller.getSubject();
  
     this.delivers = [ SELECT 
     d.Name, 
      d.Partner_Package__c, d.Price__c, d.Price_Override__c, 
      Professional_Services_Partner_Contract__c, d.Id, d.QTY__c
      FROM 
      Professional_Services_Partner_Line_Items__c d 
      WHERE 
      d.Professional_Services_Partner_Contract__c = :proj.id ];
 }
 
 
public pagereference insertmethod()
                {
                Professional_Services_Partner_Line_Items__c cc= new Professional_Services_Partner_Line_Items__c();
                cc.Professional_Services_Partner_Contract__c = proj.id;
                insert cc;
                return null;
                }
                    
  public Professional_Services_Partner_Line_Items__c[] getDeliverables() { 
  return this.delivers; 
  
 } 
 
 // Action Method called from page button
 public pagereference saveChanges() { 
  upsert this.delivers;
  pageRef.setRedirect(true); 
return pageRef;
 }
 
 // Action Method called from page link
 public pagereference newDeliverable() { 
  Professional_Services_Partner_Line_Items__c d = new Professional_Services_Partner_Line_Items__c();
  d.Professional_Services_Partner_Contract__c =this.proj.id; 
  delivers.add( d );
  getDeliverables();
  return null;
 } 
 
 public pagereference DeleteAccount()   
{      
// if for any reason we are missing the reference       
if (SelectedAccountId == null) 
{               
return null;      
}           
// find the account record within the collection      
Professional_Services_Partner_Line_Items__c tobeDeleted = null;      
for(Professional_Services_Partner_Line_Items__c a : this.delivers)       
if (a.Id == SelectedAccountId) 
{         
tobeDeleted = a;          
break;       
}            
//if account record found delete it      
if (tobeDeleted != null) 
{       
Delete tobeDeleted;      
}
pageRef.setRedirect(true); 
return pageRef;
}        

  
 // class variables

PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl()); 
 

 Professional_Services_Partner__c proj;
 Professional_Services_Partner_Line_Items__c[] delivers; 
 public string SelectedAccountId { get; set; }
}


Best,
Brian
 

Best Answer chosen by Brian Cherry FWI
Lars NielsenLars Nielsen
Here is the first place we need to start:
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm

All Answers

Lars NielsenLars Nielsen
We should be able to give you some tips Brian - do you have anything started? It took me years to get into this habbit but I now start with my Test class and work backwork. It's a bit slower than if you were working with say the tools you had available for C# but still helps to make sure all the use cases the class needs are covered. Just give us a sense of where you are - i.e. have something that is not working or starting with a blank slate.
Brian Cherry FWIBrian Cherry FWI
Hi Lars,

Right now on a blank slate. I'm not sure if I need to call my methods directly for testing or how it works. Or if I can simply pull queries on my object, make sure something matches, insert a record in my object, check the details etc...

 
Lars NielsenLars Nielsen
Here is the first place we need to start:
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm
This was selected as the best answer
Brian Cherry FWIBrian Cherry FWI
Thanks!