如何为导入 CSV 文件编写 Apex 测试 Class?

How to write an Apex Test Class for Importing a CSV File?

你好!我无法测试笔尖class..你能帮我完成这个任务吗?

通过组件,我们将发送到服务器的CSV文件的ID传输到顶点class,之后我们的顶点class创建站点,如果之前有none ,并根据 CSV 文件的记录更新传感器列表(插入/更新)。

控制器Class

public inherited sharing class lwnReadCsvFileController {

    @AuraEnabled
    public static list<Sensor__c> readCSVFile(Id idContentDocument){
        list<Sensor__c> lstSensToInsert = new list<Sensor__c>();
        if(idContentDocument != null) {
            
            // getting File Data based on document id 
            ContentVersion objVersion = [SELECT Id, VersionData FROM ContentVersion WHERE ContentDocumentId =:idContentDocument];
            // split the file data
            list<String> lstCSVLines = objVersion.VersionData.toString().split('\n');

            //creating Basic Stations records
            List<Base_Station__c> createdBSRec = [SELECT Name, Id  From Base_Station__c];
            List<Base_Station__c> newBSRec = new List<Base_Station__c>();
            Set<String> uniqueSet = new Set<String>();
            for ( Integer i = 1; i < lstCSVLines.size(); i++ ) {
                integer coincidences = 0; 
                list<String> csvRowData = lstCSVLines[i].split(',');
                for ( Base_Station__c rec : createdBSRec ) {
                    if (csvRowData[0] == rec.Name) {
                        coincidences++;
                    }
                }
                if ( coincidences == 0 ) {
                    uniqueSet.add(csvRowData[0]);
                }
            }
            List<String> uniquelist = new List<String>(uniqueSet);
            for (integer i = 0; i < uniquelist.size() ; i++) {
                Base_Station__c newBS = new Base_Station__c(Name = uniquelist[i]);
                NewBSRec.add(newBS);
            }
            upsert newBSRec;
            

            //creating Sensor records
            for(Integer i = 1; i < lstCSVLines.size(); i++){
                Sensor__c objRec = new Sensor__c();
                list<String> csvRowData = lstCSVLines[i].split(',');
                string tempBase = csvRowData[0];
                objRec.Name = csvRowData[1];
                objRec.Base_Station__c = [SELECT Id From Base_Station__c Where Name = :tempBase ].id;
                objRec.Sensor_ID__c = integer.valueof(csvRowData[1]);
                objRec.Status__c = csvRowData[2];
                objRec.Sensor_Model__c = csvRowData[3];
                lstSensToInsert.add(objRec);
            }
                if(!lstSensToInsert.isEmpty()) {
                    List<Sensor__c> createdSenRec = [SELECT Name, Sensor_ID__c  From Sensor__c];
                    for (Sensor__c sens : lstSensToInsert ) {
                        integer coincidences = 0;
                        for (Sensor__c rec : createdSenRec ) {
                            if (sens.Sensor_ID__c == rec.Sensor_ID__c) {
                                sens.id = rec.id;
                                update sens;
                                coincidences++;
                            }
                        }
                        if ( coincidences == 0 ) {
                            insert sens;
                        }
                    }
                }
        }
        return lstSensToInsert;    
    }
}

测试Class 100%覆盖率

@isTest
public class IwnReadCsvFileControllerTest {    
    public static String str = 'BASE STATION,SENSOR ID,STATUS,SENSOR MODEL \n' +
        'Leeds,1,Enabled ,R8 \n' +
        'Glasgow Central,2,Enabled,R8';
@isTest
    public static void testReadCSVFile(){
    Base_Station__c newBS = new Base_Station__c(Name = 'Leeds');
    upsert newBS;
    Sensor__c newSensor = new Sensor__c (Name = '1', Sensor_Id__c = 1, Status__c = 'Enabled', Base_Station__c = newBs.id);
    insert newSensor;
    ContentVersion contentVersionInsert = new ContentVersion(
            Title = 'Test',
            PathOnClient = 'Test.csv',
            VersionData = Blob.valueOf(str),
            IsMajorVersion = true
    );
    insert contentVersionInsert;
    Id getId = [Select ContentDocumentId From ContentVersion Where Id =:contentVersionInsert.id and isLatest=true].ContentDocumentId;
    List<Sensor__c> result = lwnReadCsvFileController.readCSVFile(getId);
}   
}

您的单元测试 class 正在通过 ContentVersion ID:

List<Sensor__c> result = lwnReadCsvFileController.readCSVFile(contentVersionInsert.Id);

但是您的 class 将此 ID 视为 ContentDocument ID:

public static list<Sensor__c> readCSVFile(Id idContentDocument){
    if(idContentDocument != null) {
        ContentVersion objVersion = [
            SELECT Id, VersionData 
            FROM ContentVersion 
            WHERE ContentDocumentId =:idContentDocument
        ];

您的测试 class 需要查询新插入的 ContentVersionContentDocumentId 并将该 Id 传递给您的 class 被测。