You need to sign in to do that
Don't have an account?

Testing Visualforce page
hello
I am new in salesforce and I want to write a test class for a visual force page. I have created a test class but i have only 33% coverage.
I have a select option and i don't know how cover that part of code.
any help will be appreciated.
This is my page
<apex:page standardController="Aspirante__c" extensions="MultiSelectController"> <apex:sectionHeader title="Estatus" /> <apex:form > <apex:pageBlock title="Aspirante" mode="edit"> <apex:outputText value="{!Aspirante__c.Estatus__c}" rendered="false"/> <apex:pageBlockButtons location="both"> <apex:commandButton value="Guardar" action="{!save}" /> <apex:commandButton value="Cancelar" action="{!cancel}" /> </apex:pageBlockButtons> <apex:pageMessages /> <apex:pageBlockSection title="Modificación de Estatus" columns="1"> <apex:pageBlockSectionItem > <apex:outputLabel value="Estatus" for="cbxlevel1"/> <apex:outputPanel styleClass="requiredInput" layout="block"> <apex:outputPanel styleClass="requiredBlock" layout="block"/> <apex:selectList value="{!selectedLevel1}" id="cbxlevel1" size="1" required="false"> <apex:selectOptions value="{!level1items}"/> <apex:actionSupport event="onchange" rerender="cbxlevel2"/> </apex:selectList> </apex:outputPanel> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
VF Page
public with sharing class MultiSelectController { // reference for the standard controller private ApexPages.StandardController controller {get; set;} // the record that is being edited private Aspirante__c opp; // the values of the selected items public string selectedLevel1 {get; set;} public List<selectOption> level1Items { get {List<selectOption> options = new List<selectOption>(); if (opp.Estatus__c == Null) { options.add(new SelectOption('New','New')); options.add(new SelectOption('Waiting','Waiting')); options.add(new SelectOption('Interview','Interview')); } if (opp.Estatus__c=='New') { options.add(new SelectOption('Waiting','Waiting')); options.add(new SelectOption('Interview','Interview')); options.add(new SelectOption('rejected','rejected')); } if (opp.Estatus__c=='Waiting') { options.add(new SelectOption('Interview','Interview')); //options.add(new SelectOption('Waiting','Waiting')); options.add(new SelectOption('rejected','rejected')); } return options; } set; } public MultiSelectController(ApexPages.StandardController controller) { //initialize the stanrdard controller this.controller = controller; // load the record this.opp = (Aspirante__c)controller.getRecord(); // preselect the current values for the record selectedLevel1 = opp.Estatus__c; } public PageReference save() { // set the selected values to the record before saving String respaldOpp; respaldOpp = opp.Estatus__c; opp.Estatus__c =selectedLevel1; try { update(opp); } catch(System.DMLException e) { ApexPages.addMessages(e); return null; } PageReference opp= new ApexPages.StandardController(opp).view(); opp.setRedirect(true); return opp; } }
Test Class
@isTest public class MultiSelectControllerTest{ public static testMethod void MultiSelectControllerTest() { PageReference pageRef = Page.selectEstatus; Test.setCurrentPage(pageRef); Aspirante__c aspirante = new Aspirante__c(Name='Tests', CorreoElectronico__c='tst@mail.com', Estatus__c='New'); insert aspirante; ApexPages.standardController controller = new ApexPages.standardController(aspirante); MultiSelectController pag = new MultiSelectController(controller); pag.save(); } }
I don't think you're ever calling your level1Items list in your test method. You can get to that with pag.getLevel1Items().
But even then, you're only testing an aspirante with Estatus__c = 'New', so that's only going to test 1/3 of that get method. Create two more aspirantes with the two otther Estatus__c values, then repeat the test again for those values.
Hi Scott_VS thanks for reply
I tried to call my level1Items as you told me (red line) but I have the next error:
Method does not exist or incorrect signature: [MultiSelectController]..getLevel1Items()
I know that i have to test the other values for Estatus__c too.