0

Below is my controller and test class, i am getting an error while compiling test class, how can i use controller object in test class? Controller

public class ERFormController
{
    public Event_Request__c t1{get;set;}
    public String stateCode {get;set;}

   public List<npe5__Affiliation__c > SchoolList{get;set;}
  // public List<SelectOption> mySchools {get;set;}


    public ERFormController(ApexPages.StandardController controller)
    {
        t1 = new Event_Request__c();
        stateCode = '';
        getdetail();
    }

    public PageReference save() {



        if (validate()) {
      String edit_start_time,edit_end_time;
      String event_starttime = t1.Start_Time__c+':'+t1.Start_Time_mm__c+' '+t1.Start_Time_Meridian__c;
      String event_endtime = t1.End_Time__c+':'+t1.End_Time_mm__c+' '+t1.End_Time_Meridian__c;
      String stime = t1.Start_Date__c+' '+event_starttime;
      String etime = t1.End_Date__c+' '+event_endtime;
      t1.State__c = stateCode;
      System.debug('Start Time in Date Time:'+t1.StartTime__c);
      t1.StartTime__c = DateTime.parse(stime);
      t1.EndTime__c = DateTime.parse(etime);
      insert t1; 
      ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));
      PageReference pg = new PageReference('/Event_list');
      pg.setRedirect(true);
      return pg;  
        } else {
          return null;
        }


    }

    public PageReference cancel()
    {
        PageReference pg = new PageReference('/Event_list');
        return pg;
    }




}

Test Class

@isTest(SeeAllData=true)
public class ERFormControllerTest{

    @isTest static void testFormintsert(){
        ApexPages.StandardController sc = new ApexPages.StandardController(Event_Request__c);

        Event_Request__c t = new Event_Request__c();
        ERFormController obj = new ERFormController(sc);
        obj.t1=t;


        t.EventName__c = 'Test';
        t.EventType__c = 'On Site Training';
        t.Start_date__c = '02/27/2016';
        t.Start_Time__c = '3';
        t.Start_Time_mm__c = '30';
        t.Start_Time_meridian__c = 'PM';
        t.End_date__c = '02/26/2016';
        t.End_Time__c = '4';
        t.End_Time_mm__c = '30';
        t.End_Time_meridian__c = 'PM';
        t.Timezone__c = 'pacific';
        t.Description__c = 'Testing';
        t.City__c  = 'Chicago';
        t.State__c = 'IL';
        t.Zip__c = '60616';
        t.Need_Volunteers__c = 'No';

 obj.save();
}
}
2
  • 4
    Best practice is to not use seealldata=true. May yield deployment failures
    – cropredy
    Commented Feb 29, 2016 at 17:39
  • can you post the exact error please?
    – mkorman
    Commented Feb 29, 2016 at 17:44

1 Answer 1

1

You need to pass a record to the constructor. I have renamed some objects to be more clear:

  • t => request
  • sc => controller
  • obj => extension

Event_Request__c request = new Event_Request();
// request.SomeField__c = 'Some Value'
// etc.

ApexPages.StandardController controller = new ApexPages.StandardController(request);
ERFormController extension = new ERFormController(controller);
3
  • Tried that but i am getting an error like variable does not exist Commented Feb 29, 2016 at 17:39
  • I guess that in your context, you would have to do ApexPages.StandardController sc = new ApexPages.StandardController(t); after you have declared your variable t
    – mkorman
    Commented Feb 29, 2016 at 17:45
  • Ahh, works fine now, Thx Commented Feb 29, 2016 at 17:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .