You need to sign in to do that
Don't have an account?
James 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.
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.
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....
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
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....
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.
Also - just realized ... in your test class - make sure you also test the saveAttendance method.
That should get you a few more lines of coverage.