You need to sign in to do that
Don't have an account?
Raffus
How to get for loop covered in test method
How to cover a for loop in this below test method
@AuraEnabled public static string getCasesData(String caseId) { try { String query = 'SELECT Id,(SELECT Id, ContentDocumentId FROM ContentDocumentLinks), CaseNumber,Issue_category__c, Issue_Sub_Category__c, rand_App_Customer__r.Party_Id__c,rand_App_Customer__r.Customer_Mobile__c, Current_Location__Latitude__s, Current_Location__Longitude__s, LocationName__c, Owner.Name, Description,CreatedDate FROM Case WHERE Id =:caseId'; query += ' ORDER BY CreatedDate DESC LIMIT 1'; List<Case> cases = Database.query(query); CaseInfoWrapper caseInfo = new CaseInfoWrapper(); set<Id> contentDocId = new set<Id>(); for (Case caseRecord : cases) { caseInfo.description = caseRecord.Description; caseInfo.longitude = caseRecord.Current_Location__Latitude__s; caseInfo.latitude = caseRecord.Current_Location__Longitude__s; caseInfo.locationName = caseRecord.LocationName__c; caseInfo.category = caseRecord.Issue_category__c; caseInfo.subCategory = caseRecord.Issue_Sub_Category__c; caseInfo.partyId = caseRecord.rand_App_Customer__r.Party_Id__c; caseInfo.mobile = caseRecord.rand_App_Customer__r.Customer_Mobile__c; if (caseRecord.ContentDocumentLinks.size() > 0) { for (ContentDocumentLink cdl : caseRecord.ContentDocumentLinks) { contentDocId.add(cdl.ContentDocumentId); } } } list<id> contectVersionID = new list<id>(); map<Id, ContentDistribution> contectDMap = new map<Id, ContentDistribution>(); for (ContentDistribution conDistribution : [SELECT Id, ContentDocumentId, ContentDownloadUrl FROM ContentDistribution WHERE ContentDocumentId IN:contentDocId ORDER BY CreatedDate ASC]) { contectVersionID.add(conDistribution.ContentDocumentId); contectDMap.put(conDistribution.ContentDocumentId, conDistribution); } list<FileInfoWrapper> fileInfo = new list<FileInfoWrapper>(); caseInfo.contentDocumentIds = contectVersionID; for (ContentVersion cv : [SELECT Id, ContentDocumentId, FileExtension, FileType, PathOnClient, VersionData FROM ContentVersion WHERE ContentDocumentId IN:contectVersionID ORDER BY CreatedDate ASC]) { FileInfoWrapper fileData = new FileInfoWrapper(); fileData.base64Data = String.valueOf(cv.VersionData); fileData.contentVersionId = cv.Id; fileData.fileName = cv.PathOnClient; if (contectDMap.containsKey(cv.ContentDocumentId)) { fileData.publicUrl = contectDMap.get(cv.ContentDocumentId).ContentDownloadUrl; fileData.contentDistributionId = contectDMap.get(cv.ContentDocumentId).Id; } fileInfo.add(fileData); } caseInfo.files = fileInfo; System.debug('caseInfo ##' + JSON.serialize(caseInfo)); return JSON.serialize(caseInfo); } catch (Exception e) { throw new AuraHandledException(e.getMessage()); } }I have written the below test class
@IsTest public static void getCasesDataTest(){ Case caseInfo = new Case(); caseInfo.description = 'test'; caseInfo.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('rand_App').getRecordTypeId(); insert caseInfo; ContentVersion contentVersion = new ContentVersion( Title = 'Penguins', PathOnClient = 'Penguins.jpg', VersionData = Blob.valueOf('Test Content'), IsMajorVersion = true ); insert contentVersion; List<ContentDocument> documents = [ SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument ]; //create ContentDocumentLink record ContentDocumentLink cdl = New ContentDocumentLink(); cdl.LinkedEntityId = caseInfo.id; cdl.ContentDocumentId = documents[0].Id; cdl.shareType = 'V'; insert cdl; randAppController.getCasesData(caseInfo.id); }But not covering the for loop that is
for (ContentVersion cv : [SELECT Id, ContentDocumentId, FileExtension, FileType, PathOnClient, VersionData FROM ContentVersion WHERE ContentDocumentId IN:contectVersionID ORDER BY CreatedDate ASC]) { FileInfoWrapper fileData = new FileInfoWrapper(); fileData.base64Data = String.valueOf(cv.VersionData); fileData.contentVersionId = cv.Id; fileData.fileName = cv.PathOnClient; if (contectDMap.containsKey(cv.ContentDocumentId)) { fileData.publicUrl = contectDMap.get(cv.ContentDocumentId).ContentDownloadUrl; fileData.contentDistributionId = contectDMap.get(cv.ContentDocumentId).Id; } fileInfo.add(fileData); }
I hope in the test class contentdistribution record is not inserted. Can you check with below test class.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
All Answers
I hope in the test class contentdistribution record is not inserted. Can you check with below test class.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
Can you help me with this, please - https://developer.salesforce.com/forums/ForumsMain?id=9062I000000UjUCQA0