for 循环的 Apex 测试类未涵盖需要做什么

问题描述 投票:0回答:1
public class ItemTypeWrapper {
    @AuraEnabled
    public Id ItemTypeId ;
    @AuraEnabled
    public String ItemType;
    public ItemTypeWrapper(Id ItemTypeId, String ItemType) {
        this.ItemTypeId = ItemTypeId;`enter code here`
        this.ItemType = ItemType;
    }
} 
//method to fetch details of the Item type from the sObject POS_Item__c
@AuraEnabled(cacheable=true)
public static List<ItemTypeWrapper> getItemTypeList_Apex()
{
    //variable to hold the account details
    objAccount=getAccount();
    //to fetch the profile details of the logged in user
    String profileName=[Select Id,Name from Profile where Id=:userinfo.getProfileId()].Name;
    //instance is created itemTypeResults of type List<POS_Item__c >
    List<POS_Item__c> itemTypeResults = new List<POS_Item__c>();
    //to hold the unique Item type ids 
    Set<Id> uniqueItemTypeIds = new Set<Id>();
    //check if the profileName is salesmanager
    if(profileName == folioSalesManagerProfile)
    {
       //fetch the item type details for salesmanager profile where marketing only is false
       itemTypeResults = [SELECT Id, Type_of_Item__r.Item_Type__c,Type_of_Item__c FROM POS_Item__c WHERE 
                                   Account__c=:objAccount.Id AND Active__c=true AND Item_ID__c!=null AND (Inventory_Seasonal_Program__c='Inventory' OR Inventory_Seasonal_Program__c='Both') AND Marketing_Only__c=false
                                   AND Logical_Invenory_Stock__c > 0 ORDER BY Type_of_Item__r.Item_Type__c];
    }
    else
    {
        itemTypeResults = [SELECT Id, Type_of_Item__r.Item_Type__c,Type_of_Item__c FROM POS_Item__c WHERE 
                                   Account__c=:objAccount.Id AND Active__c=true AND Item_ID__c!=null AND (Inventory_Seasonal_Program__c='Inventory' OR Inventory_Seasonal_Program__c='Both')
                                   AND Logical_Invenory_Stock__c > 0 ORDER BY Type_of_Item__r.Item_Type__c];
    }
    //instance is created itemTypeWrappers of type List<ItemTypeWrapper>
    List<ItemTypeWrapper> itemTypeWrappers = new List<ItemTypeWrapper>();
    //loop through the itemTypeResultsForAFacility , for each
    for (POS_Item__c result : itemTypeResults ) {
        //get the Item type record id and store it in a container itemTypeId
        Id itemTypeId = (Id)result.get('Type_of_Item__c');
        // Check if itemTypeId does not contains in the container uniqueItemTypeIds , if yes
        if (!uniqueItemTypeIds.contains(itemTypeId)) {
            //add the ItemType to the container ItemType
            String ItemType = result.Type_of_Item__r.Item_Type__c;
            //Create a new ItemTypeWrapper instance and add it to the list.
            itemTypeWrappers.add(new ItemTypeWrapper(itemTypeId, ItemType));
            //Add 'itemTypeId' to 'uniqueItemTypeIds' 
            uniqueItemTypeIds.add(itemTypeId);
        }
    }

    //return the itemTypeWrappers
    System.debug('itemTypeWrappers----'+itemTypeWrappers);
    return itemTypeWrappers;
}    

这是我的测试课

@isTest 公共类测试类{

@isTest
private static void addToCartFunctionalityTestMethod ()
{
    Account testAccount=[SELECT Id FROM Account LIMIT 1];
    Brand__c testBrand=[SELECT Id FROM Brand__c LIMIT 1];
    Item_Type__c testItemtype=[SELECT Id FROM Item_Type__c LIMIT 1];
    List<POS_Item__c> testPosItems=[SELECT Id FROM POS_Item__c];
    
    Test.startTest();
    ApexClass.getBrandList_Apex();
    ApexClass.getItemTypeList_Apex();
    Test.stopTest();
}
@testSetup static void setupData() {
    List<Account> testAccts = new List<Account>();
    Account acct = new Account(Name='demo');
    insert acct ;
    Contact contact=new Contact(LastName='Testing',AccountId=acct.Id);
    insert contact;
    Profile profile=[SELECT Name FROM Profile WHERE Name='Customer Community Plus User' LIMIT 1];
    
    User communityUser = new User(Alias = 'standt', Email='[email protected]', 
                                  EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                                  LocaleSidKey='en_US', ProfileId = profile.Id, 
                                  TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]',ContactId=contact.Id);
    
    insert communityUser;
    
    Brand__c brand = new Brand__c (Brand_Name__c='BrandNameSelect',Active__c=true,Account__c=acct.Id);
    insert brand;
    
    Item_Type__c itemType= new Item_Type__c(Item_Type__c='itemTypeSelect',Active__c=true,Account__c=acct.Id);
    insert itemType;
    POS_Item__c posItem= new POS_Item__c (Item_No__c='Test-No-01',Item_Name__c='testPosItemName',Available_Stock__c=10,
                                          Account__c=acct.Id,Brand__c=brand.Id,Type_of_Item__c=itemType.Id,Active__c=true);
    insert posItem;
    
    POS_Item__c posItem2= new POS_Item__c (Item_No__c='Test-No-02',Item_Name__c='testPosItemName',Available_Stock__c=10,
                                           Account__c=acct.Id,Brand__c=brand.Id,Type_of_Item__c=itemType.Id,Active__c=true);
    insert posItem2;
}

}

测试类包含对象所需的所有虚拟数据,但仍然没有覆盖唯一项目类型的 for 循环,请帮助我测试类覆盖率

我尝试过使用假测试类覆盖率,但现在我不希望使用假覆盖率

提前致谢

salesforce code-coverage apex apex-code lwc
1个回答
0
投票

出现这种情况是因为测试数据不正确。设置测试数据时,您将使用某些值创建此 POS_Item__c 对象,但在实际方法中检索同一对象时,您正在查找不存在的数据。具体来说,

(Inventory_Seasonal_Program__c='Inventory' OR Inventory_Seasonal_Program__c='Both')
条件在您的测试数据上不成立。

所以在设置测试数据时,应该是这样的:

POS_Item__c posItem= new POS_Item__c (Item_No__c='Test-No-01',
Item_Name__c='testPosItemName',Available_Stock__c=10,
Account__c=acct.Id,Brand__c=brand.Id,Type_of_Item__c=itemType.Id,Active__c=true, Inventory_Seasonal_Program__c='Inventory');
    
insert posItem;
© www.soinside.com 2019 - 2024. All rights reserved.