You need to sign in to do that
Don't have an account?
neil_edwards_fcca1.389781386123482E12
Wrapper Class test class - how to replicate marking a record as being selected in Test class
Hi, I'm struggling to find a way to replicate selecting a record in a visualforce page. Without somehow making the record in the test class selected nothing is being checked against the test code, Any help greatly appreciated!
e in the test class.
Extract from Test Class // Set-up of Test Records here // Set up visualforce parameters PageReference pageRef = Page.CloneDeal; PageRef.getParameters().put('id',Deal.id); Test.setCurrentPage(pageRef); // create an instance of the controller Checkbox_class myPageCon = new Checkbox_class(); test.starttest(); mypageCon.getselected(); mypageCon.getselectedfee(); mypageCon.getselectedpay(); mypageCon.getselectediss(); mypageCon.getselectedtar(); mypageCon.getselectedaq(); myPageCon.getPayments(); myPageCon.getFees(); myPageCon.getPayments(); myPageCon.getIssuer(); myPageCon.getTargets(); myPageCon.getaq(); myPageCon.getex(); myPageCon.getDeals(); myPageCon.doClone(); test.stoptest();
e in the test class.
Extract from Visualforce page <apex:page controller="Checkbox_Class" Tabstyle="Deal_Protocol__c"> <apex:stylesheet value="/sCSS/21.0/sprites/1297816277000/Theme3/default/gc/versioning.css" /> <apex:sectionHeader title="Clone Deal: "/> <apex:form id="theform" > <apex:PageBlock > <apex:PageBlockButtons > <apex:commandButton action="{!doClone}" value="Clone this Deal" /> <apex:commandButton action="{!CloseFrm}" value="Cancel & Return" /> </apex:PageBlockButtons> <apex:pageBlockSection Title="Transaction to be Cloned - Check box to select item(s) to clone"> <apex:dataTable value="{!deals}" var="a" columnswidth="50px,50px" cellpadding="4" border="1"> <apex:column > <apex:facet name="header"> <apex:inputCheckbox > <apex:actionSupport event="onclick" action="{!GetSelected}" onsubmit="checkAll(this)" rerender="Selected_PBS"/> </apex:inputCheckbox> </apex:facet> <apex:inputCheckbox value="{!a.selected}" id="checkedone"> <apex:actionSupport event="onclick" action="{!GetSelected}" rerender="Selected_PBS"/></apex:inputCheckbox> </apex:column> <apex:column headervalue="Deal Name" value="{!a.acc.Name}" /> <apex:column headervalue="Deal Type" value="{!a.acc.Agreement_Type__c}" /> </apex:dataTable> </apex:pageBlockSection> <apex:pageBlockSection Title="List of Fees that can be cloned (does not include deleted fees)"> <apex:dataTable value="{!fees}" var="f" columnswidth="50px,50px" cellpadding="4" border="1"> <apex:column > <apex:inputCheckbox value="{!f.selectedf}" id="checkedone"> <apex:actionSupport event="onclick" action="{!GetSelectedfee}" rerender="Selected_PBS"/></apex:inputCheckbox> </apex:column> <apex:column headervalue="Fee Name" value="{!f.fee.Name}" /> <apex:column headervalue="Playbook Category" value="{!f.fee.Playbook_Category__c}" /> <apex:column headervalue="Playbook Ref" value="{!f.fee.Playbook_Ref__c}" /> </apex:dataTable> </apex:pageBlockSection>
Extract from Apex class public class Checkbox_Class { String DealID = ApexPages.currentPage().getParameters().get('id'); List<DealWrapper> DealList = new List<DealWrapper>(); List<FeeWrapper> FeeList = new List<FeeWrapper>(); List<Deal_protocol__c> selectedDeal = new List<Deal_protocol__c>(); List<Fees__c> selectedFee = new List<Fees__c>(); public List<DealWrapper> getDeals() { for(Deal_protocol__c a : [select Id, Name,Agreement_Type__c from Deal_protocol__c where id=:dealid limit 1]) DealList.add(new DealWrapper(a)); return DealList; } public List<FeeWrapper> getFees() { for(Fees__c f : [select Id, Name,Playbook_Category__c,Playbook_Ref__c from Fees__c where Deal_protocol_ref__c =:dealid and deleted__c=false order by playbook_category__c ASC]) FeeList.add(new FeeWrapper(f)); return FeeList; } public PageReference getSelected() { selectedDeal.clear(); for(DealWrapper accwrapper : DealList) if(accwrapper.selected == true) selectedDeal.add(accwrapper.acc); return null; } public PageReference getSelectedFee() { selectedfee.clear(); for(FeeWrapper fwrapper : FeeList) if(fwrapper.selectedf == true) selectedfee.add(fwrapper.fee); return null; } public class DealWrapper { public Deal_protocol__c acc{get; set;} public Boolean selected {get; set;} public DealWrapper(Deal_protocol__c a) { acc = a; selected = false; } } public class FeeWrapper { public Fees__c fee{get; set;} public Boolean selectedf {get; set;} public FeeWrapper(fees__c f) { fee = f; selectedf = false; } }
mypageCon.DealList.get(0).selected = true;//select the first item using the members
myPageCon.getDeals.get(0).selected = true;//select the first item using the getter
All Answers
Deal_c testDeal = new Deal__c();
testDeal.field = yadda;
...
insert testDeal;
Deal_Protocol__c testProtocol = new Deal_Protocol__c();
...
testProtocol.Deal_protocol_ref__c = testDeal.id;
etc, etc, etc
don't worry about it being inserted into your production. The testing protocol is smart enough to to carve out a "separate" space for test record and delete them after the test is done. It's great because you can set up all the appropriate scenarios but it's tedious in that you have to set them up by hand.
You can also load test data from csvs you store as static resources using
Test.loadData();
You can learn a bit more on this here
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_load_data.htmI
I favor doing it code and creating some factory classes to help create objects with common settings. That one you only have one spot to see what the parameters of the test are.
Thanks
mypageCon.DealList.get(0).selected = true;//select the first item using the members
myPageCon.getDeals.get(0).selected = true;//select the first item using the getter
Thanks very much DevArrah for your prompt reply