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
laytro1978laytro1978 

Help on testing script

Hi,

 

Can someone please give me a hand on the test method for this piece of APEX.

 

Any help would be much appreciated.

 

I have made a start in red below.

 

Thanks

 

Ross

 

 

public class TimeEntry {

    public List<Time_Slot__c> slots {get; set;}
    
    public TimeEntry(){        
        slots = new List<Time_Slot__c>();
        slots.add(new Time_Slot__c());
    }

    public void addrow() {
        slots.add(new Time_Slot__c());
    }
           
    public void removerow(){
        Integer i = slots.size();
        slots.remove(i-1);
    }
           
    public PageReference save() {
        insert slots;
        PageReference home = new PageReference('/a00/o');
        home.setRedirect(true);
        return home;
    }
    
    public static testMethod void TimeEntry() {

    //setup test data  
    Time_Slot__c T = new Time_Slot__c();
    insert t;
    
    //test main controller action 
    List<Time_Slot__c> slots;
          
    }       
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
ericszulcericszulc

Should look something more like this. Instantiate the class, excercise the methods, and make assertions. 

 

public static testMethod void TimeEntryTest()

{

  TimeEntry t = new TimeEntry();

  System.assertEquals(t.slots.size(),0);

 

t.addRow();

  System.assertEquals(t.slots.size(),1);

 

  t.removeRow();

  System.assertEquals(t.slots.size(),0);

 

  t.addRow();

  Pagereference p = t.save();

}

All Answers

ericszulcericszulc

Should look something more like this. Instantiate the class, excercise the methods, and make assertions. 

 

public static testMethod void TimeEntryTest()

{

  TimeEntry t = new TimeEntry();

  System.assertEquals(t.slots.size(),0);

 

t.addRow();

  System.assertEquals(t.slots.size(),1);

 

  t.removeRow();

  System.assertEquals(t.slots.size(),0);

 

  t.addRow();

  Pagereference p = t.save();

}

This was selected as the best answer
laytro1978laytro1978

Thanks for this I understand what I need to do.

 

Cheers

 

R