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
Adelchi PelizzoAdelchi Pelizzo 

Test class for VF controller extension

global class getipController {


    global static String ipAddress {get; set;}
    
    global InfoForm__c  Info = new InfoForm__c();
    
    
    global getipController(ApexPages.StandardController controller) {
    
        this.Info = (InfoForm__c)controller.getRecord();
        
        ipAddress = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');
        
        Info.Ip__c = IpAddress;


    }
 }
I am struggling to make sense with testing this visualforce controller extension. Any help to build a test clas will be very much appreaciated.
Thank you.
Himanshu ParasharHimanshu Parashar
Hi Adelchi,

Following code should work for you.
 
InfoForm__c in= new InfoForm__c;
insert in;
ApexPages.currentPage().getHeaders().put('X-Salesforce-SIP', '127.0.0.1');
ApexPages.StandardController sc = new ApexPages.StandardController(in);
GetipController con = new GetipController(sc);
system.assertequals('127.0.0.1',con.ipAddress);


Thanks,

Himanshu

Adelchi PelizzoAdelchi Pelizzo
Thank you Himanshu,
I am getting - Compile Error: unexpected token: 'in' - line 2. 
Adelchi PelizzoAdelchi Pelizzo
@IsTest

private class getIpController_Test{


InfoForm__c info = new InfoForm__c();
insert info;
ApexPages.currentPage().getHeaders().put('X-Salesforce-SIP', '127.0.0.1');
ApexPages.StandardController sc = new ApexPages.StandardController(info);
GetipController con = new GetipController(sc);
system.assertequals('127.0.0.1',con.ipAddress);


}

Even if I do this I get - Compile Error: expecting right curly bracket, found 'insert' at line 7 column 0
V_PV_P
@IsTest

private class getIpController_Test
{
 
 static testMethod void Infotest()
 {
    InfoForm__c info = new InfoForm__c();
    insert info;
    ApexPages.currentPage().getHeaders().put('X-Salesforce-SIP', '127.0.0.1');
    ApexPages.StandardController sc = new ApexPages.StandardController(info);
    GetipController con = new GetipController(sc);
    system.assertequals('127.0.0.1',con.ipAddress);    
 }

}
Himanshu ParasharHimanshu Parashar
Hi Adelchi,

tthat code was not working because code should be insdie the test method as it is shown in following code
 
@IsTest

private class getIpController_Test{

static testMethod void myTest() {
InfoForm__c info = new InfoForm__c();
insert info;
ApexPages.currentPage().getHeaders().put('X-Salesforce-SIP', '127.0.0.1');
ApexPages.StandardController sc = new ApexPages.StandardController(info);
GetipController con = new GetipController(sc);
system.assertequals('127.0.0.1',con.ipAddress);

}
}

Thanks,
Himanshu