function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Clara GauntClara Gaunt 

Test class for a custom controller - aura component

Tried what feels like everything and I'm totally stuck. Any help would be really appreciated:

My custom controller:

public class submitFormCustomObjectController {
 
    
    @AuraEnabled
    public static List<Robot_Tracking_Entry__c> getRobot(Robot_Tracking_Entry__c insertRobot){
        insert insertRobot;
        
        return [select Id, Performance_Month__c,NGM_AMR_Requests__c,TQ_TFM__c,dual_supplier_deappoint_process__c,Supplierless_auto_appoint_deappoint__c,market_sector_changes__c,TSEd2__c,JVOD_JNOC__c,TQ_TFM_Power_BI__c,TSE_R3__c,TSE_R_3__c,TSEd4__c, TSE_c_A__c, Finance_accruals__c,IMS__c, Aged_Debt__c,Gosp_import__c, AMR_Photos__c, Customer_Invoicing_Billing__c, DM_Reads__c, GPS_Job_Queue__c, GPS_line_Queue__c, AMR_Certificate_uploads__c, Meter_Pick_up__c, Site_Husbandry_Monitoring__c,AMR_Stop_Deap__c, Site_Husbandry_Remedials__c,NGM_Validation_report__c,NGM_TSE_SLG_1__c, Standard_Work__c,Comments__c,NGM_Report_Distribution__c,    NGM_TSE_Creation__c, NGM_meter_validations__c, Working_days__c, Adhoc_GPS_Checks_Pulsing_check__c from Robot_Tracking_Entry__c];
    }
    
}


My js controller:
( {
    submitrobotInfo : function(component, event, helper) {
        
        var isValidate = true;        
        var firstName = component.find('fName');        
        var firstNameVal = component.find('fName').get('v.value');        
        if($A.util.isUndefinedOrNull(firstNameVal) || $A.util.isUndefined(firstNameVal) || $A.util.isEmpty(firstNameVal)){
            firstName.set('v.errors',[{message:'Performance Month is Required'}]);
            isValidate = false;
        }else{
            firstName.set('v.errors',null);
        }        
        
        if(isValidate){
             var action = component.get('c.getRobot');
             var postData =  component.get('v.robotInfoAc');                
             action.setParams({'insertRobot': postData});
            
            action.setCallback(this, function(response) {            
            var state = response.getState();  
            if (state === 'SUCCESS') {                
                var stringItems = response.getReturnValue();
                component.set('v.robotItrt',stringItems);
                component.set('v.robotInfoAc', null);       
                
                //Show the success toast message
                var toastEvent = $A.get('e.force:showToast');
                toastEvent.setParams({
                    'title':'Success',
                    'type':'success',
                    'message':'Robot Entry submitted successfully.',                        
                });
                toastEvent.fire();   
                // Close the action panel
        var dismissActionPanel = $A.get('e.force:closeQuickAction');
        dismissActionPanel.fire();
            
            }
                
        }); 
        
        $A.enqueueAction(action);
            
        }
        
  

},

        })

My component:
<aura:component controller="submitFormCustomObjectController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:lightningQuickActionWithoutHeader,lightning:actionOverride,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" >
    <aura:attribute name="robotInfoAc" type="Robot_Tracking_Entry__c" default="{'sobjectType': 'Robot_Tracking_Entry__c'}"/>
     <aura:attribute name="robotItrt" type="Robot_Tracking_Entry__c[]"/>    
    <div class="slds">
      <h1 align="center" > Add New Robot Performance Record - Overview</h1>   
        <br></br>
      
            <div class="slds-grid slds-grid_align-center slds-gutters">
                
                    <div class="slds-col slds-size_6-of-12">
//Plus many more input fields
</div>
    </div>
        
        <div class="slds-text-align--center slds-m-top--medium">
            <button class="slds-button slds-button--brand" onclick="{!c.submitrobotInfo}">Submit</button>
        </div>
    
<br/><br/>

  

          
    </div>
    
          
</aura:component>

When I am writing the test class, I am running into bother. Have therefore only created the test data as follows, but whatever method I try just doesn't work

@isTest
public class testsubmitformcontroller {
public static void testsubmitFormCustomObjectController(){
       Robot_Tracking_Entry__c re = new Robot_Tracking_Entry__c();
        re.NGM_AMR_Requests__c = 20;
        re.Comments__c = 'Hello';
       
       Test.StartTest(); 
   submitFormCustomObjectController testsubmit = new submitFormCustomObjectController;
   testsubmit.getRobot(re insertRobot){
        insert insertRobot;
    }
 
}

Help!!!

 
Best Answer chosen by Clara Gaunt
AnkaiahAnkaiah (Salesforce Developers) 
Hi Clara,

Please try with below code.
@isTest
public class testsubmitformcontroller {
private static testmethod  void testsubmitFormCustomObjectController(){
       Robot_Tracking_Entry__c re = new Robot_Tracking_Entry__c();
        re.NGM_AMR_Requests__c = 20;
        re.Comments__c = 'Hello';
insert re;

submitFormCustomObjectController.getRobot(re);
 
}
If this helps, Please mark it as best answer.

Thanks!!
 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Clara,

Please try with below code.
@isTest
public class testsubmitformcontroller {
private static testmethod  void testsubmitFormCustomObjectController(){
       Robot_Tracking_Entry__c re = new Robot_Tracking_Entry__c();
        re.NGM_AMR_Requests__c = 20;
        re.Comments__c = 'Hello';
insert re;

submitFormCustomObjectController.getRobot(re);
 
}
If this helps, Please mark it as best answer.

Thanks!!
 
This was selected as the best answer
Clara GauntClara Gaunt
Thanks that great. It's taken the coverage to 66% however. The line which isn't being covered is the retrun line as follows:
 return [select Id, Performance_Month__c,NGM_AMR_Requests__c,TQ_TFM__c,dual_supplier_deappoint_process__c,Supplierless_auto_appoint_deappoint__c,market_sector_changes__c,TSEd2__c,JVOD_JNOC__c,TQ_TFM_Power_BI__c,TSE_R3__c,TSE_R_3__c,TSEd4__c, TSE_c_A__c, Finance_accruals__c,IMS__c, Aged_Debt__c,Gosp_import__c, AMR_Photos__c, Customer_Invoicing_Billing__c, DM_Reads__c, GPS_Job_Queue__c, GPS_line_Queue__c, AMR_Certificate_uploads__c, Meter_Pick_up__c, Site_Husbandry_Monitoring__c,AMR_Stop_Deap__c, Site_Husbandry_Remedials__c,NGM_Validation_report__c,NGM_TSE_SLG_1__c, Standard_Work__c,Comments__c,NGM_Report_Distribution__c,    NGM_TSE_Creation__c, NGM_meter_validations__c, Working_days__c, Adhoc_GPS_Checks_Pulsing_check__c from Robot_Tracking_Entry__c];
 
AbhinavAbhinav (Salesforce Developers) 
Check this :

https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

Thanks!
Clara GauntClara Gaunt
Update: I removed the insert re; and am no longer getting an insert error and am achieving 100% coverage