如何为多选查找页面编写测试类

时间:2019-01-16 05:09:38

标签: apex visualforce

我在编写测试课时非常困难。该扩展名使用户可以从子项(Contingency__c)中选择多个记录,并将其添加到帐户相同的父项的相关列表(Billing_Change_form__c)。它包含一个包装器类。我不知道该如何获得保险。

这是我到目前为止提出的测试课程

@isTest
public class bcfTest {
public static testMethod void adBill(){
    Billing_Change_form__c testBill = new Billing_Change_Form__c();
    testBill.Name = 'Test name';
    insert testBill;

   Billing_Change_form__c testBill2 = new Billing_Change_Form__c();
    testBill2.Name = 'Test name2';
    insert testBill2;

     Contingency__c caseTest = new Contingency__c();
    caseTest.Name = 'test Case';
    caseTest.Contingency__c = 'IN BILLING';
    caseTest.VENDOR_INVOICE__r.Account_del__r.id='1234';
    insert caseTest;

   Contingency__c caseTest2 = new Contingency__c();
    caseTest2.Name = 'test Case2';
    caseTest2.Contingency__c = 'IN BILLING';
    caseTest2.VENDOR_INVOICE__r.Account_del__r.id='5678';
    insert caseTest2;

    Account accTest = new Account();
    accTest.Name = 'Test Account';
    insert accTest;

    Test.startTest();
   PageReference pageRef = Page.bcTest; // Add your VF page Name here
   pageRef.getParameters().put('id', String.valueOf(testBill.Id));
   Test.setCurrentPage(pageRef);


    List<wrapperCase> wrapList = new List<wrapperCase>();
    List<Contingency__c> selCase = new List<Contingency__c>();

   for(Contingency__c b : [Select id, name, 
                           contingency__c, VENDOR_INVOICE__r.Account_del__r.Id from Contingency__c
                           where (VENDOR_INVOICE__r.Account_del__r.Id = :accTest.Id) ]){
                               wrapList.add(New wrapperCase(b));
                           }


   for(wrapperCase WATest: wrapList){
       WATest.check = true;
   }


    System.assertEquals(testBill.Id, caseTest.Bill_Form__c);
    System.assertEquals('Test name',testBill.Name);
    System.assertEquals('IN BILLING', caseTest.CONTINGENCY__c);

    test.stopTest();
}
public class wrapperCase{
    public contingency__c con{get;set;}
    public boolean check{get;set;}
    public wrapperCase(contingency__c c){
        con = c;
        check = false;           
    }
}

}

我的控制器扩展名:

      public with sharing class test2 {
      //Declare varialbles
      public string lookup{get;set;}
      public list<conCase> caseList{get;set;}
      public boolean allbool{get;set;}
      public string inputValue{get;set;}
      public boolean bool{get;set;}
      public set<id> caseIds{get;set;}
      Billing_Change_form__c[] accID = [Select account__c from 
      Billing_Change_Form__c where id = 
      :System.currentPagereference().getParameters().get('id')];


      ApexPages.StandardController controller;

       //Constructor
       public test2(ApexPages.StandardController con){
        controller = con;
        caseList = new list<conCase>();
        bool = false;
        caseIds = new Set<id>();
    }


    //Event on clicking the checkbox
    public void inIt(){
        List<Contingency__c> selectedCase = new list<Contingency__c>();
        lookUp = '';
        for(conCase conObj : caseList){
            if(conObj.check != False){
                system.debug('conObj.con'+ conObj.con);
                selectedCase.add(conObj.con);
                lookUp += conObj.con.name + ', ';
                system.debug('lookup:'+ lookup);
                caseIds.add(conObj.con.id);
                bool = true;
            }
               If(conObj.check != true){
                caseIds.remove(conObj.con.id); }   
    }
    }

    //Displaying the records 
    public List<conCase> getShow(){
        caseList = new list <conCase> ();

        for(Contingency__c coObj : [Select id, name,billing_type__c, report_description__c, discovery_amount__c,
                        billing_begins_date__c, contingency__c, VENDOR_INVOICE__r.Account_del__r.Id from Contingency__c
            where (VENDOR_INVOICE__r.Account_del__r.Id = :accID[0].Account__c)  AND (Bill_form__c = null)])

        {
            caseList.add(new conCase(coObj, Bool));
        }

        return caseList;
    }

    //Event on saving the selected records
    public PageReference mySave(){
        list<Contingency__c> updateSelectedCase = new list<Contingency__c>();
        id billId = ApexPages.currentPage().getparameters().get('id');
        System.debug('Bill ID is' + billID);
        for(Contingency__c co:[select id, name, bill_form__c from Contingency__c where id = :caseIds ]){
            co.bill_form__c = billId;
            updateSelectedCase.add(co);
        }
        update updateSelectedCase;
        return null;
    }

    //Event When the cancel button is hit
    public void closePopup(){
        bool = false;
    }
    public void add(){
        bool = true;
    }

    //Wrapper class for the selected records
       public class conCase{
        public contingency__c con{get;set;}
        public boolean check{get;set;}
        public conCase(contingency__c c, boolean boo){
            con = c;
            check = boo;           
        }
    }

}   

我的VisualForce页面:

     <apex:page showHeader="False" standardController="Billing_Change_Form__c" 
     extensions="test2">
      <script>
     function closeWindow(){      
            window.opener.location.href="/{!$CurrentPage.parameters.id}";
            window.top.close();
      }
     </script>

    <apex:form >
    <apex:PageBlock title="CaseFindings" id="counter">

                    <apex:pageBlockButtons >
                      <apex:commandButton value="Save" action="{!mySave}" onComplete="closeWindow();"/>   
                      <apex:commandButton value="Cancel" onclick="window.top.close();" immediate="true" action="{!closePopup}"/>  
                </apex:pageBlockButtons>

            <apex:pageBlockTable value="{!show}" var="e" title="show">

            <apex:column >
                <apex:inputCheckbox value="{!e.check}" />
    <apex:actionSupport event="onclick" action="{!inIt}" rerender="none"/>
                </apex:column>

                <apex:column value="{!e.con.Name}"/>
                <apex:column value="{!e.con.DISCOVERY_AMOUNT__c}"/>
                <apex:column value="{!e.con.BILLING_BEGINS_DATE__c}"/>
                <apex:column value="{!e.con.REPORT_DESCRIPTION__c}"/>
                <apex:column value="{!e.con.CONTINGENCY__c}"/>
            </apex:pageBlockTable>
        </apex:PageBlock>
    </apex:form>
</apex:page>

希望获得标准的75%的覆盖率。如果有人可以帮助我,我将非常感激!谢谢!

0 个答案:

没有答案