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

Help with Test Code for Simple VS Controller
Hi guys,
I write test code for triggers and VS pages that actually do something. I can't seem to grasp the concept of how you write a test code for a VS page that simple displays a little table. Can someone help?
All I do is use the above code in a VS page that displays the data. That is it!
Any help woudl be greatly appreciated. Thanks.
I write test code for triggers and VS pages that actually do something. I can't seem to grasp the concept of how you write a test code for a VS page that simple displays a little table. Can someone help?
public class OpplineItemHist{ private List<Oppty_Line_Item_History__c> Hist; private Opportunity Opp; public OpplineItemHist(ApexPages.StandardController controller) { this.Opp= (Opportunity)controller.getRecord(); } public List<Oppty_Line_Item_History__c> getHist() { Hist= [SELECT Action__c,Action_Date__c,name, Action_Initiated_By__c,Action_Initiated_By__r.Alias,Fixed_Variable__c,Modification__c, Modification_Type__c,Opportunity__c,Oppty__c,CurrencyISOCode,Product_Family__c,mod__c,Product_Name__c,Quantity__c,Dropped__c,TotalPrice__c,Sales_Price__c,Line_Item__c,Delta_Total_Price__c, Site__c FROM Oppty_Line_Item_History__c WHERE Opportunity__c =:opp.id ORDER BY Action_Date__c DESC]; return Hist; } }
All I do is use the above code in a VS page that displays the data. That is it!
Any help woudl be greatly appreciated. Thanks.
If you want to write a Test Class for the above class:
@isTest
public class OpplineItemHistTest {
@isTest
public static testController() {
// Here create an opportunity record.
Opportunity opp = new Opportunity();
opp.Name = 'Test Opp';
opp..CloseDate = System.today();
opp.StageName = 'Prospect';
insert opp;
ApexPages.currentPage().getParameters().put( 'id', opp.Id ); // This is optional
OpplineItemHist oppItemHistController = new OpplineItemHist( new ApexPages.StandardController(opp) );
List<Oppty_Line_Item_History__c> oppLineItemHistList = oppItemHistController.getHist();
}
}
Please do let me know if you need more information about it.
Regards,
Mahesh
All Answers
The best way to test this controller is to set up data in the test class; Some that meet criteria, and some that do not. Then create a new instance of the controller and use an assert on the size of the "Hist" list.
Hope that helps!
If you want to write a Test Class for the above class:
@isTest
public class OpplineItemHistTest {
@isTest
public static testController() {
// Here create an opportunity record.
Opportunity opp = new Opportunity();
opp.Name = 'Test Opp';
opp..CloseDate = System.today();
opp.StageName = 'Prospect';
insert opp;
ApexPages.currentPage().getParameters().put( 'id', opp.Id ); // This is optional
OpplineItemHist oppItemHistController = new OpplineItemHist( new ApexPages.StandardController(opp) );
List<Oppty_Line_Item_History__c> oppLineItemHistList = oppItemHistController.getHist();
}
}
Please do let me know if you need more information about it.
Regards,
Mahesh