+ Start a Discussion
krishnagkrishnag 

test class for a controller

 

hi,

 

i am new to this test classes concept. Can anybody give an example for writing a test class for a controller.

 

public PageReference psignup() {
    emailerror=false;
        emailacept=false;
        List<guestuser__c> ldtls=[select Id from guestuser__c where Email__c=:email];
        
        if(ldtls.size()>0)
        {
            emailerror=true;
            emailacept=false;
            //uid exist
        }
        else
        {
            if(pwd==rpwd)
            {
                emailerror=false;
                emailacept=false;
                guestuser__c gu=new guestuser__c();
                gu.First_Name__c=fname;
                gu.Password__c=pwd;
                gu.Email__c=email;
                gu.Title__c = job;
                gu.Email_Optout__c = echeck;
                gu.Last_Name__c = lname;
                gu.Company__c = comp;
                gu.Street__c = street;
                gu.State__c = state;
                gu.City__c = city;
                gu.Country__c = ctry;
                gu.Zipcode__c = zip;
                
                
                Lead ld=new Lead();          
                ld.FirstName =fname;
                ld.LastName=lname;
                ld.Company=comp;
                ld.Street=street;
                ld.State=state;
                ld.city=city;
                ld.PostalCode=zip;
                ld.country=ctry;
                ld.Title=job;
                ld.Email=email;
                ld.HasOptedOutOfEmail=echeck;
                
                
                try{
                insert ld;
                insert gu;
                Pagereference pageref=new Pagereference('/apex/thankq');     
                    pageref.setRedirect(true);
                    return pageref;
                }
                catch(Exception e)
                {
                    system.debug(e);
                }
            }
            else
            {
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.WARNING, 'PASSWORD MISS-MATCH');
                ApexPages.addMessage(myMsg);
            }
        }
        return null;
    }


    public Boolean echeck { get; set; }

    public String rpwd { get; set; }

    public String pwd { get; set; }

    public void checkavlble() {
        String lusername= Apexpages.currentPage().getParameters().get('mail');
        List<guestuser__c> chkldtls=[select Id from guestuser__c where Email__c=:email];
        if(chkldtls.size()>0)
        {
            emailerror=true;
            emailacept=false;
        }
        else
        {           //try another
            emailerror=false;
            emailacept=true;
        }
    }


    public Boolean emailacept { get; set; }

    public Boolean emailerror { get; set; }

    public String email { get; set; }

    public String job { get; set; }

    public String ctry { get; set; }

    public String zip { get; set; }

    public String state { get; set; }

    public String city { get; set; }

    public String street { get; set; }

    public String comp { get; set; }

    public String lname { get; set; }

    public String fname { get; set; }
}

 

i need to write the test class for this controller to deploy the page.can anybody help me with an example. 

 

Best Answer chosen by Admin (Salesforce Developers) 
jeffdonthemic2jeffdonthemic2

Try setting your values like:

 

controller.fname = 'abc';

 

instead of like:

 

controller.fname('abc');

 

Jeff Douglas

Appirio, Inc.

http://blog.jeffdouglas.com

 

Author: The Salesforce Handbook

All Answers

jeffdonthemic2jeffdonthemic2
krishnagkrishnag

hi i wrote the test class for the above controller and i am getting the error.

 

 

Error

Error: Compile Error: Method does not exist or incorrect signature: [gusignup].fname(String) at line 13 column 4

 

 

 

 

@isTest
private class gusignupTests
{
 public static testMethod void testMyController()
 {
   PageReference p = Page.thankq;
   Test.setCurrentPage(p);
   
   gusignup controller = new gusignup();
   String nextPage = controller.psignup().getURL();
   
   controller = new gusignup();
   controller.fname('abc');
   controller.lname('xyz');
   controller.email('abc@xyz.com');
   controller.job('developer');
   controller.street('golf');
   controller.city('okc');
   controller.state('ok');
   controller.ctry('usa');
   controller.echeck(True);
   controller.comp('zurich');
   controller.zip('48326');
   nextPage = controller.psignup().getURL();
   
   System.assertEquals('apex/thankq',nextPage);
   Lead[]leads = [select Id,Email from lead where Company = 'zurich'];
   System.assertEquals('abc@xyz.com', leads[0].Email);
   
 }
}

 

 

jeffdonthemic2jeffdonthemic2

Try setting your values like:

 

controller.fname = 'abc';

 

instead of like:

 

controller.fname('abc');

 

Jeff Douglas

Appirio, Inc.

http://blog.jeffdouglas.com

 

Author: The Salesforce Handbook

This was selected as the best answer
krishnagkrishnag

thanks its working jeff