You need to sign in to do that
Don't have an account?
Chris Moynihan
Test Case Help
I am trying to write a test case for my page/controller. I've written test code for triggers before without a lot of trouble, but it's been a long time and I am having trouble getting my tests written. Below is my code, as well as where I am with my test code so far.
The page/controller let's me add multiple entries for a custom object so I can enter a bunch at once and then save them.
Any help and direction is much appreciated.
Controller:
Test Code:
Page:
The page/controller let's me add multiple entries for a custom object so I can enter a bunch at once and then save them.
Any help and direction is much appreciated.
Controller:
public class MultiAddSEActivities { // Holds the SE Activity records to be saved public List<SE_Activities__c>recList = new List<SE_Activities__c>(); // List of the recClass public List<recClass> actRecords {get;set;} // Indicates the row to be deleted public String selectedRowIndex {get;set;} // # of records in the recClass list public Integer count = 0; // Save the records by adding the elements in the record class list to recList, return to the same page public PageReference Save() { PageReference pr = new PageReference('/a00/o'); for(Integer j = 0;j<actRecords.size();j++) { recList.add(actRecords[j].seact); } insert recList; pr.setRedirect(True); return pr; } // Add a row public void Add() { count = count+1; // Call to the recClass constructor recClass objRecClass = new recClass(count); // Add the record to the recClass list actRecords.add(objRecClass); } // Add duplicate of last row public void dupe() { if (count == 0){ return; }else{ // Create new Record Class record recClass dupeRecord = new recClass(count+1); // Get duplicate record from temp list dupeRecord.seact = actRecords[count-1].seact.clone(false,true,false,false); // Add the record to the record class list actRecords.add(dupeRecord); count = count + 1; } } // Delete row public void Del() { // Find the matching record in the list and remove it Integer j = 0; while (j < actRecords.size()) { if(actRecords.get(j).recCount == selectedRowIndex) { actRecords.remove(j); count = count - 1; } else { j++; } } // Reset recCount for each record in the list Integer r = 0; while (r < actRecords.size()) { actRecords[r].recCount = String.valueOf(r+1); r++; } } // Constructor public MultiAddSEActivities() { //ApexPages.StandardController ctlr actRecords = new List<recClass>(); Add(); selectedRowIndex = '0'; } // Record Class public class recClass { // recCount acts as a index for a row. This will be helpful to identify the row to be deleted public String recCount {get;set;} public SE_Activities__c seact {get;set;} // Record Class Constructor public recClass(Integer intCount) { // Set the index recCount = String.valueOf(intCount); // Create a new SE Activity seact = new SE_Activities__c(); } } }
Test Code:
@isTest public class controllerTests { public static testMethod void testMultiAddSEActivitiesPage(){ PageReference pg = Page.MultiAddSEActivities; Test.setCurrentPage(pg); MultiAddSEActivities controller = new MultiAddSEActivities(); String nextPage = controller.Save().getUrl(); // Instantiate a new controller with all params on the page controller = new MultiAddSEActivities(); controller.Add(); controller.dupe(); controller.Del(); public List<SE_Activities__c> testList = new List<SE_Activities__c>(); // ????? nextPage = controller.Save().getUrl(); } }
Page:
<apex:page controller="MultiAddSEActivities" id="thePage"> <apex:form > <apex:pageblock id="pb" > <apex:pageBlockButtons > <apex:commandbutton value="Add" action="{!Add}" rerender="pb1"/> <apex:commandbutton value="Duplicate Last" action="{!Dupe}" rerender="pb1"/> <apex:commandbutton value="Save" action="{!Save}"/> </apex:pageBlockButtons> <apex:pageblock id="pb1"> <apex:repeat value="{!actRecords}" var="e1" id="therepeat"> <apex:panelGrid columns="8"> <apex:panelGrid headerClass="Name" width="15px"> <apex:facet name="header">Delete</apex:facet> <apex:commandButton value="x" action="{!Del}" rerender="pb1"> <apex:param name="rowToBeDeleted" value="{!e1.recCount}" assignTo="{!selectedRowIndex}"></apex:param> </apex:commandButton> </apex:panelGrid> <apex:panelGrid title="SPD"> <apex:facet name="header">Activity Date</apex:facet> <apex:inputfield value="{!e1.seact.Activity_Date__c}"/> </apex:panelGrid> <apex:panelGrid > <apex:facet name="header">Opportunity</apex:facet> <apex:inputfield style="width:250px" value="{!e1.seact.OpportunityLink__c}"/> </apex:panelGrid> <apex:panelGrid > <apex:facet name="header">Type</apex:facet> <apex:inputfield value="{!e1.seact.Type__c}"/> </apex:panelGrid> <apex:panelGrid > <apex:facet name="header">Hours</apex:facet> <apex:inputfield style="width:30px" value="{!e1.seact.Hours__c}"/> </apex:panelGrid> <apex:panelGrid > <apex:facet name="header">Comments</apex:facet> <apex:inputfield style="width:250px; height:12px" value="{!e1.seact.Comments__c}"/> </apex:panelGrid> <apex:panelGrid > <apex:facet name="header">Onsite</apex:facet> <apex:inputfield value="{!e1.seact.Onsite__c}"/> </apex:panelGrid> </apex:panelgrid> </apex:repeat> </apex:pageBlock> </apex:pageblock> </apex:form> </apex:page>
I modified your test case by adding assertions in the test case to make it complete.
All Answers
I modified your test case by adding assertions in the test case to make it complete.
I had to modify that ever so slightly (remove the @ symbol and move the check to a variable, fix the count values, change the selected row index to a string value) but then it worked with 97% coverage. Below is my final code.
If I set up a trigger to do validation on the SE_Activities__c object to make sure a date field has a date in it, my validation rule is triggered with this test method and it fails.
Do I have to create a test activity with a date here and how do I keep the test on the Save call from failing?
In test case:
The problem is that with the trigger, the controllerTests method fails, since upon calling the Save() test line, there is no date.
Error Message: Stack Trace: Which refers to this line in the test method and the insert recList; in the page code: I'm not sure how to get the test of the Save method to have dates in it.
You can either add a default value through recClass constructor by adding:
seact.Activity_Date__c = today();
or in ADD() , add:
objeRecClass.seact.Activity_Date__c = today();
and in DUPE(), add:
dupeRecord.seact.Activity_Date__c = today();
Basically, you need to assign value to Activity_Date__c for each record.
I just need to add a date for the test code so it doesn't trigger that error. However, since I'm not actually creating a record with the test code, I don't see how I can do that.
I am guessing I need to create a record with data in it and somehow use that to test the Save() method, but I can't seem to figure out how to do that and assign it to the controller in the test.
That was the missing link for me.