You need to sign in to do that
Don't have an account?
Ronaldo Costa 8
Aura Test Class - Simple DML help
Hello, I have the following simple code:
But my current class is only covering 66%:
Can you please help? Upon deployment is says as follows:
SaveAndNew
C_Testtest_saveDetails_UseCase1System.DmlException: Insert failed. First exception on row 0 with id 00Qf400000IsyiqEAB; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Stack Trace: Class.SaveAndNewC.saveDetails: line 6, column 1 Class.SaveAndNewC_Test.test_saveDetails_UseCase1: line 14, column 1
public class SaveAndNewC { @AuraEnabled public static Id saveDetails(Lead regForm1){ // DML operation to save Lead Details INSERT regForm1; return regForm1.Id; } }
But my current class is only covering 66%:
@isTest private class SaveAndNewC_Test{ @testSetup static void setupTestData(){ test.startTest(); Lead lead_Obj = new Lead(LastName = 'LastName836', FirstName = 'First211', Company = 'Company565', Phone = '54343-13889', Status = 'Unqualified', IsConverted = false, IsUnreadByOwner = false); Insert lead_Obj; test.stopTest(); } static testMethod void test_saveDetails_UseCase1(){ List<Lead> lead_Obj = [SELECT LastName,FirstName,Company,Phone,Status,IsConverted,IsUnreadByOwner from Lead]; System.assertEquals(true,lead_Obj.size()>0); SaveAndNewC obj01 = new SaveAndNewC(); SaveAndNewC.saveDetails(lead_Obj[0]); //lead_Obj[0] } }
Can you please help? Upon deployment is says as follows:
SaveAndNew
C_Testtest_saveDetails_UseCase1System.DmlException: Insert failed. First exception on row 0 with id 00Qf400000IsyiqEAB; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Stack Trace: Class.SaveAndNewC.saveDetails: line 6, column 1 Class.SaveAndNewC_Test.test_saveDetails_UseCase1: line 14, column 1
I did walkthrough your code and found some minor issues.
Please try this code:
================================================================
Class:
public class SaveAndNewC {
@AuraEnabled
public static Id saveDetails(Lead regForm1){
// DML operation to save Lead Details
INSERT regForm1;
return regForm1.Id;
}
}
================================================================
Test Class:
@isTest
public class SaveAndNewC_Test{
static testMethod void setupTestData(){
test.startTest();
Lead lead_Obj = new Lead(LastName = 'LastName836', FirstName = 'First211', Company = 'Company565', Phone = '54343-13889', Status = 'Unqualified', IsConverted = false, IsUnreadByOwner = false);
Id returnId = SaveAndNewC.saveDetails(lead_Obj);
System.assertEquals(true, returnId != null);
test.stopTest();
}
}
Please let me know if you have any other query. If this solution is helpful then please mark it as Best Answer.
Thanks,
Ankit Rathor
Aura Test Class - Simple DML help. This test class is 100% code coverage.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com