【问题标题】:Apex Test Class for Invocablemethods可调用方法的 Apex 测试类
【发布时间】:2020-12-29 10:05:21
【问题描述】:

我是编写 apex 的新手,尤其是在 apex 中测试类。我似乎无法找到如何在我的测试类中正确调用我的类方法。我的测试课肯定没有完成,但我只是从我的删除方法之一开始。

我似乎在我的测试类“CreateQuoteProducts_OtherOpp.DeleteQuoteProductsOTFOther(testQPOTF.id);”的第 17 行卡住了我收到一条错误消息:

方法不存在或签名不正确:来自 CreateQuoteProducts_OtherOpp 类型的 void DeleteQuoteProductsOTFOther(Id)

我想我只需要传入 OrderId,但我不太确定我做错了什么。

这是我的顶级课程

public class CreateQuoteProducts_OtherOpp{
    @invocableMethod
    public static void DeleteQuoteProductsMRFOther(List<Id> OrderId){
        List<Quote_Product_MRF__c> QuoteProductsMRFOther = [SELECT id from Quote_Product_MRF__c WHERE Order__c in : OrderId];
        if(!QuoteProductsMRFOther.isEmpty()){
            delete QuoteProductsMRFOther;
        }
    }
    public static void DeleteQuoteProductsOTFOther(List<Id> OrderId){
        List<Quote_Product__c> QuoteProductsOTFOther = [SELECT id from Quote_Product__c WHERE Order__c in : OrderId];
        if(!QuoteProductsOTFOther.isEmpty()){
            delete QuoteProductsOTFOther;
        }
    }
/* ---START--- Create Quote Products MRF ---START--- */
    public void CreateQuoteProductsMRFOther(List<Id> OrderId){
        List<AggregateResult> nrqpmrfOPOther = [
        SELECT 
        count(Id) ct,
        Order__c ord,
        sum(Total_Monthly_Recurring_Fees__c) stmr,
        sum(Monthly_Recurring_Fees__c) mr,
        sum(Discount_MRF__c) dmr
        FROM Order_Location_Package__c 
        WHERE Order__c in : OrderId AND Package__r.Monthly_Recurring_Price__c != null AND Package_Status__c = 'New'
        GROUP BY Package__c, Order__c];
        List<Quote_Product_MRF__c> nrqpmrfOther = New List<Quote_Product_MRF__c>();
        for(AggregateResult ar : nrqpmrfOPOther){
        Quote_Product_MRF__c QuoteProductMRF = New Quote_Product_MRF__c();
        QuoteProductMRF.Quantity__c = (Decimal)ar.get('ct');
        QuoteProductMRF.Order__c = (String)ar.get('ord');
        QuoteProductMRF.Total_Monthly_Recurring_Fees__c = (Decimal)ar.get('stmr');
        QuoteProductMRF.Monthly_Recurring_Fees__c = (Decimal)ar.get('mr');
        QuoteProductMRF.Discount_MRF__c = (Decimal)ar.get('dmr');
        QuoteProductMRF.Product_Type__c = 'Package';
        nrqpmrfOther.add(QuoteProductMRF);
        } 
        Insert nrqpmrfOther;
    }
/* ---END--- Create Quote Products MRF ---END--- */
/* ---START--- Create Quote Products OTF ---START--- */
    public void CreateQuoteProductsOTFOther(List<Id> OrderId){
        List<AggregateResult> nrqpotfOPOther = [
        SELECT 
        count(Id) ct,
        Order__c ord,
        sum(Total_One_Time_Fees__c) stmr,
        sum(One_Time_Fees__c) mr,
        sum(Discount_OTF__c) dmr
        FROM Order_Location_Package__c 
        WHERE Order__c in : OrderId AND Package__r.One_Time_Price__c != null AND Package_Status__c = 'New'
        GROUP BY Package__c, Order__c];
        List<Quote_Product__c> nrqpotfOther = New List<Quote_Product__c>();
        for(AggregateResult ar : nrqpotfOPOther){
        Quote_Product__c QuoteProductOTF = New Quote_Product__c();
        QuoteProductOTF.Quantity__c = (Decimal)ar.get('ct');
        QuoteProductOTF.Order__c = (String)ar.get('ord');
        QuoteProductOTF.Total_One_Time_Fees__c = (Decimal)ar.get('stmr');
        QuoteProductOTF.One_Time_Fees__c = (Decimal)ar.get('mr');
        QuoteProductOTF.Discount_OTF__c = (Decimal)ar.get('dmr');
        QuoteProductOTF.Product_Type__c = 'Package';
        nrqpotfOther.add(QuoteProductOTF);
        } 
        Insert nrqpotfOther;
    }
/* ---END--- Create Quote Products ORD ---END--- */
}

这是我正在写的测试课

@isTest
private class CreateQuoteProducts_OtherOppTest {
    private static testMethod void doTest() {
        Order_Sheet__c testOrder  = new Order_Sheet__c ();
                testOrder.Opportunity__c = 'Opportunity';
                insert testOrder;
        
Test.startTest();
        /* --- Create Quote Product OTF--- */
        Quote_Product__c testQPOTF  = new Quote_Product__c ();
                testQPOTF.Order__c = testOrder.id;
                
                insert testQPOTF;

        /* --- Run Delete Method--- */
        CreateQuoteProducts_OtherOpp.DeleteQuoteProductsOTFOther(testQPOTF.id);
        
        /* --- Query for QP OTF--- */
        list<Quote_Product__c>queriedQPOTF = [Select Id FROM Quote_Product__c WHERE Order__c = :testOrder.id];
        
        /* --- Checks for 0 records --- */
        system.assertEquals(0, queriedQPOTF.size());

        Test.stopTest();

【问题讨论】:

    标签: salesforce apex test-class salesforce-development salesforce-developer


    【解决方案1】:

    public static void DeleteQuoteProductsOTFOther(List&lt;Id&gt; OrderId) 接受 ID 列表。您尝试传递一个 ID:CreateQuoteProducts_OtherOpp.DeleteQuoteProductsOTFOther(testQPOTF.id);

    就像“整数数组”和“一个整数”是不兼容的类型。如果您不需要该列表用于其他任何内容,您可以在运行中创建它,然后在该行代码完成时将其丢弃:

    CreateQuoteProducts_OtherOpp.DeleteQuoteProductsOTFOther(new List&lt;Id&gt;{testQPOTF.id});

    这应该编译得很好

    【讨论】:

    • 完美!非常感谢您的解释。现在一切都在编译。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多