我的contoller测试类需要帮助。我在我的Production中创建了一个入站更改集,在我的Sandbox中创建了一个出站更改集。现在我正处于从22%代码覆盖率到至少75%代码覆盖率的阶段,并且之前没有这样做过。 这是我的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>
这是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;
}
}
}
和此测试代码 -
@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();
}
}
请做必要的 请指教.. 问候, 深
答案 0 :(得分:1)
为了达到75%的测试覆盖率,您的测试类需要调用至少75%的控制器类代码行。
要正确执行此操作,您需要创建多个测试方法。最佳做法是利用断言语句来确保返回值良好且有效,但不要求达到75%。
在当前代码中,您只对控制器类进行三次调用。您实例化一个新的控制器,然后调用Addrecord,然后调用取消。
您还有其他七种永远不会被调用的方法。您需要打电话给他们以获得正确的测试覆盖率。
因为您的代码缺少正确的格式,注释,有意义的变量名称(为什么它们各自使用不同的方法时有options1,option2,options3,options4,options5),或者关于发生了什么的上下文,我可以只给你一个快速而肮脏的解决方案(见下文)。但是,真正诚实的答案是您需要阅读Salesforce文档,参加一些在线培训,并阅读一般的编码标准。
这是快速而肮脏的解决方案。在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();