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
Alejandro Garcia LopezAlejandro Garcia Lopez 

how to get more coverage in my test trigger

Hello, i have a trigger in a custom object but the test only retuns 30% of coverage, i had a similar trigger and i did the same test, i got 100% of coverage with it, i don't undserstand what is missing to get more coverage
trigger VeEstado on Oferta__c (after update) {  

    for (Oferta__c c:Trigger.new)
    {
        if (c.Estado__c!=null){
integer i=[select count() from oferta__c where FechaCobro__c != null AND Estado__c =:c.Estado__c];
Estados__c l=[select id, vendidas__c, Propiedades_Disponibles__c from Estados__c where id=:c.Estado__c];
l.vendidas__c=i;
 
integer d=[SELECT count() FROM Oferta__c WHERE TiempoOferta__c >= -1500 AND PrecioVenta__c <= 5000000 
               AND Estatus__c != 'Descartada' AND FechaCobro__c = Null AND FechaPago__c != Null 
               AND ((Etapa__c IN ('Compilación expediente compra','Compra final',
                                  'Escrituras en proceso registro','Compilación expediente cliente')) 
                                OR (Etapa__c IN ('Rehabilitación') AND Invadida__c = true))
                                    AND Estado__c =:c.Estado__c ];
l.Propiedades_Disponibles__c=d;       
update l; 
    }
}
}
this is the test 
@istest 
public class VeEstadoTest {
static testMethod void VeEstadoTest()
{
Oferta__c fer = new Oferta__c();
fer.Name='fer2';
insert fer;
fer.Name='fer22';
update fer;
}
}


 
Best Answer chosen by Alejandro Garcia Lopez
Amit Singh 1Amit Singh 1
In test class you need to insert Estado__c object like below
Estado__c e = new .Estado__c();
e.Name = '';
// your other required field
insert e;
// then use e.id while inserting Oferta__c record
Oferta__c fer = new Oferta__c();
fer.Name='fer2';
fer.Estado__c = e.id
Thanks!
Amit
 

All Answers

Amit Singh 1Amit Singh 1
Ok use apex class like below.
@istest 
public class VeEstadoTest {
static testMethod void VeEstadoTest()
{
Oferta__c fer = new Oferta__c();
fer.Name='fer2';
fer.TiempoOferta__c = -1400;
fer.PrecioVenta__c = 89303;
fer.FechaCobro__c='';
fer.FechaPago__c =somevalue here can not be null;
fer.FechaPago__c = other than 'Descartada';
fer.Etapa__c = must be one of 'Compilación expediente compra','Compra final', 'Escrituras en proceso registro','Compilación expediente cliente',Rehabilitación;
fer.Invadida__c = true;
insert fer;
// You also need to insert Estado__c record .
fer.Name='fer22';
update fer;
}
}
Hope this helps :)
Thanks!
Amit Singh
 
Alejandro Garcia LopezAlejandro Garcia Lopez
i tried that but the test doesn't run, do i need to fill all that fields? the problem is that some of that fields are not writable and just with the name it gives 30% of coverage
Amit Singh 1Amit Singh 1
Yes you need to create test data as per the SOQL that you are using in your Apex Trigger and Estado__c must be a value because you had a check of Estado__c!=null
Thanks!
Amit
Alejandro Garcia LopezAlejandro Garcia Lopez
then i had to create a record in Oferta__r.Estado__c ? i tried to do that but the test doesn't run, or do a class for estado__c? how would you do that? 
Amit Singh 1Amit Singh 1
In test class you need to insert Estado__c object like below
Estado__c e = new .Estado__c();
e.Name = '';
// your other required field
insert e;
// then use e.id while inserting Oferta__c record
Oferta__c fer = new Oferta__c();
fer.Name='fer2';
fer.Estado__c = e.id
Thanks!
Amit
 
This was selected as the best answer
Alejandro Garcia LopezAlejandro Garcia Lopez
it works perfectly,  you're the best :)