I'm having trouble writing a test class for a custom controller used in an VF page. The goal is a UI allowing the creation of multiple records for a Custom Object Time_item__c. Everything works as expected, but I can't for the life of me figure out the test class.
The controller code:
public class AddmultipleTimeItemsController {
Time_Item__c TimeItem = new Time_Item__c();
public list<Time_Item__c> listTimeItem{ get; set; }
public AddmultipleTimeItemsController(){
listTimeItem=new list<Time_Item__c>();
listTimeItem.add(TimeItem);
}
public void addTimeItem(){
Time_Item__c TI = new Time_Item__c();
listTimeItem.add(TI);
}
public PageReference saveTimeItem() {
for(Integer i=0; i<listTimeItem.size(); i++) {
upsert listTimeItem;
}
return Page.Alltimeitemssaved;
}
}
The VF page:
<apex:page Controller="AddmultipleTimeItemsController">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!listTimeItem}" var="TI">
<apex:column headerValue="Account Name">
<apex:inputField value="{!TI.AccountTime__c}"/>
</apex:column>
<apex:column headerValue="Case Number">
<apex:inputField value="{!TI.CaseItem__c}"/>
</apex:column>
<apex:column headerValue="Type">
<apex:inputField value="{!TI.TypeTime__c}"/>
</apex:column>
<apex:column headerValue="Hours">
<apex:inputField value="{!TI.Hours__c}"/>
</apex:column>
<apex:column headerValue="Notes">
<apex:inputField value="{!TI.Notes__c}"/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:commandButton value="Add Time Item Row" action="{!addTimeItem}"/>
<apex:commandButton value="Save Time Items" action="{!saveTimeItem}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
I'm relatively new to coding in general, so any help is greatly appreciated.
Edit: My attempt at adapting an example test class
@isTest
public class addMultpleTestClass {
public static testMethod void testMyController() {
//PageReference pageRef = Page.Alltimeitemssaved;
// Test.setCurrentPage(pageRef);
AddmultipleTimeItemsController controller = new AddmultipleTimeItemsController();
String nextPage = controller.saveTimeItem().getUrl();
// Verify that page fails without parameters
System.assertEquals('/apex/failure?error=noParam', nextPage);
// Instantiate a new controller with all parameters in the page
Account testAccount = new Account(name='Acc');
insert testAccount;
Time_Item__c TI = new Time_Item__c(Hours__c=5.00,AccountTime__c = testAccount.Id, TypeTime__c ='Pre-Sales' );
// insert TI;
controller = new AddmultipleTimeItemsController();
controller.addTimeItem();
nextPage = controller.saveTimeItem().getUrl();
// Verify that the success page displays
System.assertEquals('/apex/Alltimeitemssaved', nextPage);
}
}
The test fails citing missing required fields (Hours__c and AccountTime__c).
Cheers!
Hours__c
andAccountTime__c
fields required fields on theTime_Item__c
object? If so, you will have to fill those values in before you can attempt to save. I'm guessing that if you leave those fields blank on your Visualforce page, you will get the same error when you attempt to save.controller.addTimeItem()
and update them by hand. Which is how the functionality behaves on the Visualforce page -- the user navigates to the page, adds a time record, then manually updates the values in the table, then clicksSave
. In tests you need to simulate this behavior. This might be easiest to accomplish with afor (Time_Item__c ti : controller.listTimeItem) { c.Hours__c = 10; c.AccountTime__c = 42; }
or the like. Please note, I do not know the actual field types, just guessing.