+ Start a Discussion
kiran k 12kiran k 12 

Test class for "for loop"

I have written a test class for trigger.the result is pass,but 0% coverage.how to achieve more than 85%?
Trigger:
trigger Rfleet_DeleteRecords_Draft on Opportunity_car_set__c(before delete) {
    set < id > BId = new set < id > ();
    set < id > OpId= new set <id>();
    if (Trigger.isDelete && Trigger.isBefore) {
        Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('RFLEET-OPP-Algeria-RT').getRecordTypeId();

        for (Opportunity_car_set__c rec: trigger.old) {
            BId.add(rec.Opportunity__c);
            OpId.add(rec.id);
            List < Opportunity > ListOpp = [select StageName, RecordTypeId from Opportunity where id = : BId];
           for (Opportunity opp: ListOpp) {
                if (opp.RecordTypeId == devRecordTypeId) {
                    if (opp.StageName == 'Qualification' || opp.StageName == 'Prospecting') {
                        rec.adderror('You cannot delete this record!');
                    }
                }
            }
        }
      
    }
     delete [SELECT id FROM Service__c where Opportunity_car_set__c =:OpId]; 
}

Test class:
@isTest
private class  Rfleet_DeleteRecords_Draft_Test{
  static testMethod void Rfleet_DeleteRecords_Draft(){
       test.startTest();    
    
        Opportunity opp= new Opportunity();
        opp.name='Prabu';
        opp.stagename='Qualification';
        opp.CloseDate=system.Today();
        insert opp;
        opp.name='kiran';
        //opp.StageName = 'Prospecting';
        update opp;
      system.assertEquals('kiran',opp.name);
      
    
        Opportunity_car_set__c oppCarSet = new Opportunity_car_set__c();
        oppCarSet.name='Opp';
        oppCarSet.Opportunity__c =opp.id;
        oppCarSet.Quantity__c =2;
        insert oppCarSet;
        oppCarSet.Quantity__c =3;
      update oppCarSet;
      system.assertEquals(3,oppCarSet.Quantity__c);
     
        
        Service__c ser = new Service__c();
        ser.Opportunity_car_set__c = oppCarSet.id;
        ser.Quantity__c = 1;
        ser.Price_HT__c = 10;
        insert ser;
      ser.Price_HT__c = 20;
      delete ser;
      
      system.assertEquals(20,ser.Price_HT__c);
      test.stopTest();
      
        
    }
}
Hanimi ReddyHanimi Reddy
You have written before delete event Apex trigger on Opportunity_car_set__c Object . But in test class you doing insert and update on Opportunity_car_set__c.
So trigger will not fire. it will give 0%.
kiran k 12kiran k 12
I have written a delete in opportunity carset object.but i got an error is "System.DmlException: Insert failed. First exception on row 0; first error: ENTITY_IS_DELETED, entity is deleted: []"
Tarun Khatri (TK)Tarun Khatri (TK)
Hello Hanimi,

Just a thought on your trigger, you have written Delete command in the last line of your trigger to delete the Opportunity_car_set__c record.
delete [SELECT id FROM Service__c where Opportunity_car_set__c =:OpId];
Now here is a catch, this trigger gets invoked when user try to delete a record. Now in this trigger, you have DML operation which again tries to delete the same record.

Seems like you need to update the trigger by removing Delete statement. After that you can add delete in your test class to cover this trigger.

-Thanks,
TK

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
kiran k 12kiran k 12
I have tried according your suggestions but i got error like  :System.DmlException: Insert failed. First exception on row 0; first error: ENTITY_IS_DELETED, entity is deleted: []  , how to rectify this error?