+ Start a Discussion
James BurnettJames Burnett 

Test Class Help for Working Class & VF Page

I am new to apex coding, and I working on a pro-bono project for a non-profit. I am having a problem writing a working test for an apex class and VF page combination that I truly believe is working as intended (tested in Sandbox - not verified by apex test class). I was hoping someone could help me clean up this test class so that I can get this function deployed.  

Here's what I have so far - 

VF Page:

<apex:page controller="AddMultipleAttendanceController" >
    
    <apex:form >
    
    <apex:pageBlock >
    
        <apex:pageBlockTable value="{!listAttendances}" var="Att">
        
            <apex:column headerValue="Client">
                <apex:inputField value="{!Att.Client__c}"/>
            </apex:column>
        
            <apex:column headerValue="Program Name">
                <apex:inputField value="{!Att.Program__c}"/>
            </apex:column>

            <apex:column headerValue="Session Week">
                <apex:inputField value="{!Att.Session__c}"/>
            </apex:column>
      
        </apex:pageBlockTable>

    <apex:pageBlockButtons >
    
        <apex:commandButton value="Add Attendances Row" action="{!addAttendance}"/>

        <apex:commandButton value="Save Attendances" action="{!saveAttendance}"/>

    </apex:pageBlockButtons>

    </apex:pageBlock>
    
</apex:form>
</apex:page>


Here is the Apex Class:

public class AddMultipleAttendanceController
{   
    Attendance__c enrollment = new Attendance__c();
     
    public List<Attendance__c> listAttendances { get; set; } 
         
    public AddMultipleAttendanceController()
    {
    listAttendances= new list<Attendance__c>();
    listAttendances.add(enrollment);
    }
    
    Public void addAttendance()
    {
    Attendance__c Att = new Attendance__c();
    listAttendances.add(Att);
    }
    public PageReference saveAttendance() {
    for(Integer i=0; i<listAttendances.size(); i++)
    {
    insert listAttendances;
    }
    return Page.Allitemssaved;
    }   
}

I cannot manage, however, to get a test class to work. I was planning on just using a single object item to test it, but maybe that wasn't working. Can anyone help me with this?

Here is all that I have managed to code for the test class (like I said, novice):

@isTest
public class AddMultipleAttendanceControllerTests {

    Private Static testmethod void AddMulitipleAttendanceControllerTests() {
        Attendance__c objAttendance = new Attendance__c();
        objAttendance.Client__c = 'a0v5B000000AHni';
        objAttendance.Program__c = 'a105B00000002j9';
        objAttendance.Session__c = 'a135B0000000MwN';
        insert objAttendance;

I would really appreciate any help with this. I'm sure there are people out there that could do this in just a few moments, but I lack the skill. Any documentation that you could add explaining the steps that I am missing would be extremely helpful. Thanks.

 
Best Answer chosen by James Burnett
JeffreyStevensJeffreyStevens
Well - your controller has some issues with it, but let's just get a working test class working for you.  In a test class - on a controller You want to define the object of the controller.  Like this....
 
AddMultipleAttendanceController amac = new AddMultipleAttendanceController();

When you do that - you've created an instance of AddMultipleAttendanceController and called it amac.  At that same time - you've also run the constructor of the controller.

Then you would want to execute the other method in your controller - addAttendance.  Do that like this....
 
amac.addAttendance();

Now as far as what you already have in your test class - that won't work when you try to take it to production.  You see - you're hard coding record ID's in there - and those IDs are going to be different between sandbox and production.  To do this correctly - you should insert records in your test class for the Client, Program, Session, objects, then you'll have record ID's to assign to the reference fields on the Attenance object. 

All Answers

JeffreyStevensJeffreyStevens
Well - your controller has some issues with it, but let's just get a working test class working for you.  In a test class - on a controller You want to define the object of the controller.  Like this....
 
AddMultipleAttendanceController amac = new AddMultipleAttendanceController();

When you do that - you've created an instance of AddMultipleAttendanceController and called it amac.  At that same time - you've also run the constructor of the controller.

Then you would want to execute the other method in your controller - addAttendance.  Do that like this....
 
amac.addAttendance();

Now as far as what you already have in your test class - that won't work when you try to take it to production.  You see - you're hard coding record ID's in there - and those IDs are going to be different between sandbox and production.  To do this correctly - you should insert records in your test class for the Client, Program, Session, objects, then you'll have record ID's to assign to the reference fields on the Attenance object. 
This was selected as the best answer
James BurnettJames Burnett
Well, you weren't lying about having issues with my controller. I've only got 60% code coverage. Thanks for your help with the test. I will work on my controller. That's the breaks, I guess. 
JeffreyStevensJeffreyStevens
Ya - you might try to re-write your controller more like this....
 
public class addMultipleAttendanceController {

  public Attendance__c enrollment {get;set;}
  public list<Attendance__c> listAttendances {get;set;}

  public addMultipleAttendanceController() {
    listAttendances = new list<Attendance__c>();
    listAttendances.add(enrollment);  
  }

  public void addAttendance() {
    Attendance__c Att = new Attendance__c();
    listAttendances.add(Att);
  }

  public void pageReference saveAttendance() {
    if(listAttendances.size()>0) {
      insert listAttendances;
    }
    return page.AllItemsSaved;
  }

}

Also - just realized ...  in your test class - make sure you also test the saveAttendance method. 
 
pageReference pr = amac.saveAttendance();

That should get you a few more lines of coverage. 
James BurnettJames Burnett
Including the "pageReference pr = amac.saveAttendance();" actually did the trick. I had 92% coverage after that. I'm still going to work on cleaning it up a little, but this was a big win for this organization. Adding multiple items at once is going to make life so much easier for them. Thanks so much for your help!