You need to sign in to do that
Don't have an account?

Test Methods
I have been trying to understand test methods but I can't seem to grasp them. Here is the controller that I have if anyone could give me some tips or explain how test methods work that would be great thanks.
Controller that needs a test method:
public class SearchController { public String Searchtxt {get; set;} public List<refWrapper> refWraps {get; set;} List<Need__c> needlist = [SELECT Name, Id, Requirement_Number__c, Bounty__c, Bounty_Other__c, Bounty_Rules__c FROM Need__c ORDER BY Name]; public Referral__c referral{ get{if(referral == null) referral = new Referral__c(); referral.Partner__c = ApexPages.currentPage().getParameters().get('id'); return referral;} set;} public PageReference SearchId() { needlist = [SELECT Name, Id, Requirement_Number__c, Bounty__c, Bounty_Other__c, Bounty_Rules__c FROM Need__c WHERE Requirement_Number__c =:Searchtxt]; if(Searchtxt == '' || Searchtxt == null) needlist = [SELECT Name, Id, Requirement_Number__c, Bounty__c, Bounty_Other__c, Bounty_Rules__c FROM Need__c ORDER BY Name]; refWraps = new List<refWrapper>(); for(Need__c N : needList){ refWrapper recWrap = new refWrapper(); recWrap.need=n; recWrap.checked = false; refWraps.add(recWrap); } return null; } public SearchController(){ refWraps = new List<refWrapper>(); for (Need__c n : needList) { refWrapper recWrap = new refWrapper(); recWrap.need=n; recWrap.checked = false; refWraps.add(recWrap); } } public PageReference ReferralSelect() { for(refWrapper ref : refWraps){ if(ref.checked){ referral.Need__c = ref.need.id; } } insert referral; PageReference tosecondpage = Page.leadstep1; tosecondpage.getParameters().put('Id',referral.Id); tosecondpage.setRedirect(true); return tosecondpage; } public class refWrapper{ public Referral__c ref {get; set;} public Need__c need {get; set;} public Boolean checked {get; set;} } public PageReference Logout(){ return Page.LeadRegistration; } }
Well it took longer then I thought it would be I got a test method that has 100% code coverage. If you can see any other way to optimize it then let me know otherwise here it is.
Test Method:
All Answers
Have you read the appropriate sections in the Apex Developer Guide?
I have but from what I have seen in the Developer Guide they use simple methods and mine are a bit more complex. Would there be anything that I would have to take into account when I am creating my own test method?
Not necessarily, you will probably have more complex data to setup.
As it says in your sig line, the more you code the more you know, so just start and see what happens :)
Well it took longer then I thought it would be I got a test method that has 100% code coverage. If you can see any other way to optimize it then let me know otherwise here it is.
Test Method:
Your test method needs to also verify that your code is working as expected. For this you will need some System.assert methods. For example verify that ReferralSelect actually inserts the referral record and returns a properly defined PageReference object.
How about this one.
Test Method:
Yes, like that, but you should do more - to cover all the expected functionality.