I am trying to write a test class for a Visualforce page, which returns a list of associated records to the contact object. The test class is throwing no errors, however is also not covering the Extension either.
I have an understanding that I have to create test data in the Test class, then call the standard controller, and then run an instance of the extension.
EDIT: this was failing due to a trigger on the same object. Note to self - CHECK TEST RESULTS outside of developer console!!
Thanks in advance!
VF:
<apex:page showHeader="false" standardStylesheets="false" standardController="contact" extensions="studentResultsExtension">
<div class="cell">
<button class="btn btn-default pull-right" onclick="selectElementContents(document.getElementById('courseEnrollments') );">
<span class="glyphicon glyphicon-copy"/> Copy Table</button>
<h1>Course Enrollment Information</h1>
<table id="courseEnrollments" class="table table-striped" style="width:100%;">
<thead class="tableHeadBlue">
<tr>
<td>Qualification</td>
<td>End Date</td>
<td>Unit Code</td>
<td>Unit Name</td>
<td>Result</td>
<td>Grade</td>
<td>Trainer</td>
</tr>
</thead>
<tbody>
<apex:repeat value="{!couEnr}" var="ce">
<tr>
<td>{!ce.Qualification__c}</td>
<td><apex:outputText value="{0, date, MMMM d',' yyyy}"><apex:param value="{!ce.Course_End_Date__c}" /></apex:outputText></td>
<td>{!ce.Unit_Code__c}</td>
<td>{!ce.Unit_Description__c}</td>
<td>{!ce.Total_Grade_Result__c}</td>
<td>{!ce.Total_Weighted_Grade__c}</td>
<td>{!ce.Course_Trainer__c}</td>
</tr>
</apex:repeat>
</tbody>
</table>
</div>
</apex:page>
APEX Extension:
public with sharing class studentResultsExtension {
public list <Course_Enrollment__c> couEnr {get;set;}
public ID contactID {get;set;}
public studentResultsExtension(ApexPages.StandardController controller) {
contactID = ApexPages.currentPage().getParameters().get('Id');
couEnr = [SELECT id,name,Course_End_Date__c,Course_Trainer__c,Qualification__c,Survey_ID__c,Total_Grade_Result__c,Total_Weighted_Grade__c,Unit_Code__c,Unit_Description__c FROM course_enrollment__c WHERE contact__c = :contactID];
}
}
Test Class
@istest
public class testStudentResultsExtension {
public static testMethod void TestStudentResults(){
test.startTest();
Id ceRecordType = [Select Id, Name from RecordType where name = 'Higher Education' limit 1].Id;
Account testAccount = new Account();
testAccount.Name='Test Company Name123';
insert testAccount;
Contact testContact = new Contact();
testContact.firstname='test';
testContact.lastname='contact';
testContact.account=testAccount;
insert testContact;
Course_enrollment__c testCourseEnrollment = new Course_Enrollment__c();
testCourseEnrollment.RecordTypeId=ceRecordType;
testCourseEnrollment.contact__r.id=testContact.Id;
insert testCourseEnrollment;
PageReference pageRef = Page.studentResults;
Test.setCurrentPage(pageRef);
pageRef.getParameters().put('id', String.valueOf(testContact.Id));
ApexPages.StandardController sc = new ApexPages.StandardController(testContact);
studentResultsExtension viewEnrollments = new studentResultsExtension(sc);
system.assert(sc != null);
List<Course_enrollment__c> couEnr = new List<Course_Enrollment__c>{};
couEnr = [SELECT id,name FROM Course_Enrollment__c WHERE Contact__r.id = :testContact.Id];
test.stopTest();
}
}
CTRL + K
, it will indent for you. Or hit the{}
button in the rich text editor.