0

我想将我的代码部署到生产中。在此 apex 代码中,我正在调用第三方 api 以获得单击按钮的机会,该按钮会触发 VF 页面中的 doSomething()。我想解决这个问题并将下面的代码推送到我的生产帐户。这是我的顶级课程代码

{
    private ApexPages.StandardController standardController;
 
    public DetailButtonController(ApexPages.StandardController standardController)
    {
         
        this.standardController = standardController;
    }
 
    public PageReference doSomething()
    {
        // Apex code for handling record from a Detail page goes here
        Id recordId = standardController.getId();
        Opportunity record = (Opportunity) standardController.getRecord();
        
        HttpRequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();
    Http http = new Http();
    req.setEndpoint('https://mergeasy.com/merge_file');
    req.setMethod('POST');
    
    //function to Convert date to mm/dd/yyy
    Date dToday = record.Closing_Date__c;
    String clos_date =  'On or before ' + dToday.month() + '/' + dToday.day() + '/' + dToday.year();
    
    Date dAcc = record.Offer_Acceptance_Date__c;
    String acc_date = dAcc.month() + '/' + dAcc.day() + '/' + dAcc.year();
    
    String str1 = '' + record.Purchase_Price__c ;
    String f_p_price = str1.SubStringBefore('.');
    
    String str2 = '' + record.Escrow_Deposit__c ;
    String e_d_price = str2.SubStringBefore('.');
    
    String str3 = '' + record.Balance__c ;
    String b_price = str3.SubStringBefore('.'); 
      
    
    if(record.Second_Seller_Name_Phone__c==null && record.Second_Seller_Email__c==null && record.Name!=null && record.Company_Profile__c!=null){
        req.setBody('seller_name='+record.Name+'&buyer_name='+record.Company_Profile__c+'&county='+record.County_Contract__c+'&street_address='+record.Left_Main__Address_1__c+'&p_price='+f_p_price+'&escrow_deposit='+e_d_price+'&title_agent='+record.Escrow_Agent_Name__c+'&title_address='+record.Escrow_Address__c+'&title_phone='+record.Escrow_Number__c+'&balance='+b_price+'&accept_date='+acc_date+'&closing_date='+clos_date+'&inspection_days='+record.Inspection_Days__c+'&special_clause='+record.Special_Clauses__c+'&doc_id=XXXXXXXXXX&doc_name=Contract.pdf&delivery_method=docusign&sign_order=true&recipient1_email='+record.Email__c+'&recipient1_name='+record.Name+'&recipient2_name='+record.Company_Profile__c+'&recipient2_email=developer.c2c@gmail.com&docusign_doc_name=Contract - Attorney Involved&email_subject=Contract:'+record.Left_Main__Address_1__c+'&email_body=Hi please sign the attached contract');
    }
    
    else if(record.Second_Seller_Name_Phone__c!=null && record.Second_Seller_Email__c!=null && record.Name!=null && record.Company_Profile__c!=null){
        String name = record.Name + ' and ' + record.Second_Seller_Name_Phone__c ;
        req.setBody('seller_name='+name+'&buyer_ame='+record.Company_Profile__c+'&county='+record.County_Contract__c+'&street_address='+record.Left_Main__Address_1__c+'&p_price='+f_p_price+'&escrow_deposit='+e_d_price+'&title_agent='+record.Escrow_Agent_Name__c+'&title_address='+record.Escrow_Address__c+'&title_phone='+record.Escrow_Number__c+'&balance='+b_price+'&accept_date='+acc_date+'&closing_date='+clos_date+'&inspection_days='+record.Inspection_Days__c+'&special_clause='+record.Special_Clauses__c+'&doc_id=XXXXXXXXXX&doc_name=Contract.pdf&delivery_method=docusign&sign_order=true&recipient1_email='+record.Email__c+'&recipient1_name='+record.Name+'&recipient2_name='+record.Second_Seller_Name_Phone__c+'&recipient2_email='+record.Second_Seller_Email__c+'&recipient3_email=developer.c2c@gmail.com&recipient3_name='+record.Company_Profile__c+'&docusign_doc_name=Contract - Normal(1S1B).pdf&email_subject=Contract:'+record.Left_Main__Address_1__c+'&email_body=Hi please sign the attached contract');
    }
    
    req.setHeader('Authorization', 'Bearer XXXXXXXXXXXXXX'); 
 try {
        res = http.send(req);
    } catch(System.CalloutException e) {
        System.debug('Callout error: '+ e);
        System.debug(res.toString());
    }
        return null;
 
    }
} 

这是测试类,它显示了 90% 的代码覆盖率。

//testClasst.apxc
@isTest
public class testClassBt {
    @isTest 
    static void testPostCallout() {
               
        System.Test.setMock(HttpCalloutMock.class, new TestClass());
         Opportunity opp = new Opportunity();
          opp.Name='Rickson Developer';
          opp.StageName='Underwrite';
          opp.CloseDate= date.newInstance(1991, 2, 21);
          opp.Closing_Date__c= date.newInstance(1991, 2, 21);
          opp.Offer_Acceptance_Date__c =date.newInstance(1991, 2, 21);
          opp.Purchase_Price__c = 1200.00;
          opp.Escrow_Deposit__c= 1200.00;
          opp.Company_Profile__c='RFTA Properties, LLC';
          opp.County_Contract__c='Orange';
          opp.Left_Main__Address_1__c='123 Main Street';
          opp.Escrow_Agent_Name__c='Test Agent';
          opp.Escrow_Address__c='123 Main street';
          opp.Escrow_Number__c='9892132382';
          opp.Inspection_Days__c=34;
          opp.Special_Clauses__c='Test';
         insert opp;
         ApexPages.StandardController standardController = new ApexPages.StandardController(opp);
        DetailButtonController strResp = new DetailButtonController(standardController);

        strResp.doSomething();
    }
}

//TestClass.apxc

@isTest
global class TestClass implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest request) {
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{"animal": {"id":1, "name":"Tiger"}}');
        response.setStatusCode(200);
        return response;
    }
}
4

1 回答 1

0

假设在验证过程中您只运行此类的测试方法,您是否尝试先在 Sandbox 中运行您的测试类?

某些 IDE 和 Salesforce 开发人员控制台本身会在单元测试执行后向您显示覆盖的行。只需按照绿线调试代码并了解引发异常的位置即可。

如果您也可以发布测试课程,我们可以为您提供更多帮助。

于 2021-05-10T19:49:51.887 回答