简体   繁体   English

测试Visualforce页面的自定义控制器

[英]Testing custom controller for visualforce page

I need help for my contoller test class.I have created an inbound change set in my Production, and an outbound in my Sandbox. 我需要有关contoller测试类的帮助。我已经在Production中创建了一个入站更改集,在我的Sandbox中创建了一个出站更改。 Now I am at the step of going from 22% code coverage to at least a 75% code coverage, and not having done this before. 现在,我正处于从22%的代码覆盖率到至少75%的代码覆盖率的步骤,并且以前没有这样做。 This is my VisualForce Page. 这是我的VisualForce页面。

<apex:page controller="MyDealsheetController" tabStyle="Dealsheet__c">
  <apex:form >
      <apex:pageMessages />
      <apex:pageBlock title="Create DealSheet">
      <apex:pageBlockButtons >
      <apex:commandButton action="{! Addrecord}" value="Create Dealsheet"/>
      </apex:pageBlockButtons>

<apex:pageBlockSection columns="2" title="Dealsheet Detail">
<apex:inputField value="{! Dealsheet.Trade_Date__c }"/>
<apex:inputField value="{! Dealsheet.Buy_Sell__c }"/> 
<apex:selectList size="1" value="{! Dealsheet.Counter_Party__c}" multiselect="false">
<apex:selectOptions value="{!CPList}"></apex:selectOptions>
</apex:selectList>
<apex:selectList size="1" value="{! Dealsheet.Pipe_Line__c}" multiselect="false">
<apex:selectOptions value="{!PipelineList}"></apex:selectOptions>
</apex:selectList>  
<apex:inputField value="{! Dealsheet.Start_Date__c }"/>
<apex:inputField value="{! Dealsheet.End_Date__c }"/>
<apex:inputField value="{! Dealsheet.Broker__c }"/>
<apex:inputField value="{! Dealsheet.Brokerage_Per_MMBTU__c }"/>
<apex:inputField value="{! Dealsheet.CP_Trader__c }"/>
<apex:inputField value="{! Dealsheet.CP_Confirm_Email__c }"/>
<apex:inputField value="{! Dealsheet.Deal_Type__c }"/>
<apex:inputField value="{! Dealsheet.Delivery_Point__c }"/>
<apex:inputField value="{! Dealsheet.Volume_MMBTU_Per_Day__c }"/>
<apex:inputField value="{! Dealsheet.PriceIndex_Name__c }"/>
<apex:inputField value="{! Dealsheet.Tradebook__c }"/>
</apex:pageBlockSection>         
<apex:PageblockSection columns="1" >
 <apex:PageBlockSectionItem >
 <apex:outputLabel value="Price Type"/>
   <apex:actionRegion >
        <apex:inputField label="Price Type" value="{!Dealsheet.Price_Type__c}">
          <apex:actionSupport event="onchange" reRender="ajaxrequest" />
           </apex:inputField>
         </apex:actionRegion>
        </apex:PageBlockSectionItem>
           </apex:PageblockSection>
         <apex:outputPanel id="ajaxrequest">  
          <apex:pageBlockSection rendered="{!Dealsheet.Price_Type__c =='Fixed'}" >
           <apex:inputField value="{!Dealsheet.Fixed_Price__c}"/>          
         </apex:pageBlockSection>
     <apex:pageBlockSection rendered="{!Dealsheet.Price_Type__c =='Floating'}" >
       <apex:inputField value="{! Dealsheet.Price_Diff__c}"  />
     </apex:pageBlockSection>

   <apex:pageBlockSection columns="1" title="Comments">
   <apex:inputField value="{! Dealsheet.Trader_Comments__c }">
   </apex:inputField>
   </apex:pageBlockSection>
   </apex:outputPanel>
  </apex:pageBlock>
 </apex:form>
</apex:page>

This is VisualForce Controller Code - 这是VisualForce控制器代码-

 public class MyDealsheetController {
 public Dealsheet__c Dealsheet;
 public MyDealsheetController()
 {
    Dealsheet= new Dealsheet__c();
 }
 public Dealsheet__c getDealsheet() {
    return Dealsheet;
 }     
 public List<selectOption> getCPList() {
 List<selectOption>  options= new List<selectOption>(); 
 for (CP__c cp :[SELECT Id, Name FROM CP__c])    
 { 
 options.add(new selectOption(cp.Id, cp.Name)); 
 }
return options; 
}   
public List<selectOption> getPipelineList() {
List<selectOption>  options1= new List<selectOption>(); 
for (NGPIPES__c pipe :[SELECT Id, Name FROM NGPIPES__c])    
{ 
options1.add(new selectOption(pipe.Id, pipe.Name)); 
}
return options1; 
}   
public List<selectOption> getCTList() {
List<selectOption> options2=new List<selectOption>(); 
for (CP_Trader__c CT :[SELECT Id, Name FROM CP_Trader__c])    
{ 
options2.add(new selectOption(CT.Id, CT.Name)); 
}
return options2; 
}  
public List<selectOption> getDPList() {
List<selectOption> options3= new List<selectOption>(); 
for (Delivery_Point__c DP :[SELECT Id, Name FROM Delivery_Point__c])    
{ 
options3.add(new selectOption(DP.Id, DP.Name)); 
}
return options3; 
} 
public List<selectOption> getBrokerList() {
List<selectOption> options4= new List<selectOption>(); 
for (CP_Broker__c Br :[SELECT Id, Name FROM CP_Broker__c])    
{ 
 options4.add(new selectOption(Br.Id, Br.Name)); 
}
 return options4; 
 }  
public List<selectOption> getPIList() {
List<selectOption> options5=new List<selectOption>(); 
for (PRICEINDEX__c PI :[SELECT Id, Name FROM PRICEINDEX__c])    
{ 
 options5.add(new selectOption(PI.Id, PI.Name)); 
}
return options5; 
}  
public PageReference Cancel()
{
 PageReference page = new PageReference('/apex/DealsheetController');
 page.setRedirect(true);
 return page;
}
public PageReference Addrecord()
{
 try {
    insert(Dealsheet);
    PageReference pageRef = ApexPages.currentPage();
    pageRef.setRedirect(true);
    return pageRef;
 }
 Catch(System.DmlException e)
 {

  ApexPages.addMessages(e);
    return null;
  }
 }   
}

and this Test code - 和此测试代码-

  @isTest
  public class TestMyDealsheetController {
  static testMethod void VerifyTestMyDealsheetController()
  {
  Dealsheet__c ds = new Dealsheet__c ();
            ds.Trade_Date__c=date.parse('01/01/2015');
            ds.Buy_Sell__c='Buy';
            ds.Counter_Party__c=''
            ds.Start_Date__c=date.parse('02/02/2015');
            ds.End_Date__c=date.parse('12/12/2015');
            ds.Volume_MMBTU_Per_Day__c=11;
        test.startTest(); 
        insert(ds);
        test.stopTest();
 ApexPages.currentPage().getParameters().put('DealsheetController','?');
 ApexPages.StandardController stdDS = new   ApexPages.StandardController(ds);
 MyDealsheetController MyDSController = new MyDealsheetController();
        MyDSController.Addrecord();
        MyDSController.Cancel();

 }
}

Please do the needfull please advice.. Regards, Deep 请做needfull请咨询。。

In order to achieve 75% test coverage, your test class needs to call at least 75% of the lines of your controller class code. 为了达到75%的测试覆盖率,您的测试类至少需要调用控制器类代码的75%的行。

To do this properly, you will need to create multiple test methods. 为了正确执行此操作,您将需要创建多种测试方法。 It is a best practice to utilize assert statements to ensure that the return values are good and valid, but it is not a requirement to reach the 75%. 最佳实践是使用assert语句来确保返回值正确且有效,但是并不需要达到75%。

In your current code, you only make three calls to your controller class. 在当前代码中,您仅对控制器类进行了三个调用。 You instantiate a new controller, then call the Addrecord, and then call Cancel. 您实例化一个新的控制器,然后调用Addrecord,然后调用Cancel。

You have seven other methods that never get called. 您还有其他七个永远不会被调用的方法。 You need to call them to get the correct test coverage. 您需要致电给他们以获得正确的测试范围。

Because your code is lacking proper formatting, comments, meaningful variable names (why is there an options1, option2, options3, options4, options5 when they are each in different methods?), or context about what is going on, I can only give you a quick and dirty solution (see below). 因为您的代码缺乏正确的格式,注释,有意义的变量名(为什么当它们分别处于不同方法中时,为什么会有options1,option2,option3,options4,options5?)或发生了什么情况,我只能给您快速而肮脏的解决方案(请参阅下文)。 However, the real honest answer is you need to read up on the Salesforce Documentation, take some of their online trainings, and read-up on coding standards in general. 但是,真实的答案是,您需要阅读Salesforce文档,接受一些在线培训,并一般地阅读编码标准。

Here is the quick and dirty solution. 这是快速又肮脏的解决方案。 Add the following below MyDSController.Cancel(); MyDSController.Cancel();下面添加以下内容MyDSController.Cancel();

Dealsheet__c dealsheet_return = MyDSController.getDealsheet();
List<selectionOption> getCPList_return = MyDSController.getPGList();
List<selectionOption> getCPList_return = MyDSController.getPipelineList();
List<selectionOption> getCPList_return = MyDSController.getCTList();
List<selectionOption> getCPList_return = MyDSController.getDPList();
List<selectionOption> getCPList_return = MyDSController.getBrokerList();
List<selectionOption> getCPList_return = MyDSController.getPIList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM