You need to sign in to do that
Don't have an account?
kiran 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(); } }
So trigger will not fire. it will give 0%.
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. 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.