+ Start a Discussion
matt.bylermatt.byler 

Testing code

I have the following code that I am trying to write a test method for and am not able to figure out how to make this work.

 

public class MyExtension
{
public Work_Order__c wo{get; set;}
opportunity opp;

public MyExtension(ApexPages.StandardController controller)
{
opp=[select id,accountId from opportunity where id=:controller.getId()];
wo = new Work_Order__c();
wo.Opportunity__c=opp.id;
if(opp.AccountId!=null)
wo.Account__c=opp.AccountId;

}
public PageReference save()
{
insert wo;

PageReference woPage = new ApexPages.StandardController(wo).view();
woPage.setRedirect(true);
return woPage;
}


public PageReference cancel()
{

PageReference woPage = new ApexPages.StandardController(wo).view();
woPage.setRedirect(true);
return woPage;
}
}

 

This code runs a visualforce page that creates a record for a custom object relating back to the opportunity and the opportunity account. Custom object is called a work order. Let me know if there is anything I can do to test this.

Starz26Starz26

here is a start for you:

 

//Create an opportunity
Opportunity opp = New Opportunity(YOURFIELDS);


            pr = Page.YOURPAGENAME;
            pr.getParameters().put('id',opp.id);
            test.setCurrentPage(pr);
            YOURPAGENAME thepage = new YOURPAGENAME(New ApexPages.StandardController(opp));

            myExtension ext = New myExtension();
          
            //test methods
            ext.save();
            ext.cancel();

 

Modify as necessary and add your systemAsserts as needed to verify the correct things are happening