function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
andyaldis1.3969086903835708E12andyaldis1.3969086903835708E12 

Test Method

I am writing a test class and am having trouble getting a mthod to test correctly.  The method looks at a couple of parameters and I am not sure how to pass the parameters to the method through my test class.  Can anyone help me?

I need to pass the SelectBox = True and the Participated = false paramerters to this method.  
    public pageReference saveContacts(){
     List<eventRelation> addeventRelation = new List<eventRelation>();
     for(cContacts c: getTeamMember()){
        IF(c.selectBox ==  true && c.participated == false){
            eventRelation ev = new eventRelation();
            ev.RelationId =  c.cId;
            ev.eventId =  eId;
            addeventRelation.add(ev);
     }
    }
    insert addeventRelation;
    pageRef.setRedirect(true);
    return pageRef;
    }
My test method.
 PageReference page = new PageReference('/apex/EventTeamMemberSelection?scontrolCaching=1&id=' + testEvent.Id);
        Test.setCurrentPage(page);
        Test.StartTest();
        ApexPages.StandardController stdCont = new ApexPages.StandardController(testEvent);
        EventTeamMemberSelection extCont = new EventTeamMemberSelection(stdCont); 
        pageReference addCon = extCont.saveContacts();
        pageReference delCon = extCont.deleteContacts();
        Test.StopTest();
Purushotham YellankiPurushotham Yellanki
Hi Andyaldis,

Always its a best practice to create your test data while writing test methods than relying on (seAllData=True). Here in this Test Class looks like you are getting sContacts from getTeamMember() method, so try creating your sContacts records with SelectBox=True and Participated=False and that should take care of checking your IF condition




Thank you
andyaldis1.3969086903835708E12andyaldis1.3969086903835708E12
The problem is that both those fields are booleans added to a wrapper class not true fields on an actual object.  I used a wrapper class to create a teble made up of information from two existing Objects and added IsSelected and Participated to the wrapper class named cContacts, so I don't think I can actually create cContact records in a test class.
Purushotham YellankiPurushotham Yellanki
Ok, in that case one alternative would be to add below code just above your IF condition and that way it should get into your eventRelation creation logic, see if it helps!
If (Test.isRunningTest()) {c.selectBox=True;c.participated=True;}




Thank you
andyaldis1.3969086903835708E12andyaldis1.3969086903835708E12
That works.  You sir are the man.
Purushotham YellankiPurushotham Yellanki
Sure and glad it helped. Please mark this as the answer if you think so!


Thank you