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
Amn12345Amn12345 

Unable to increase Test Coverage.. Pls help!!!!!

I have written a trigger to display and error if user try to create more than one record for an object.

Its working fine,

 

trigger ENT_Trigger_OnlyOneFund on Fund__c(before insert)

 { 
integer count = [select count() from Fund__c]; 
if(count>0) 

for(Fund__c L : trigger.New) 

  L.addError('There can be only one record of Fund '); 


}

--------------------------

Than I wrote a test class for the same which is showing me 13% test covegare. 

 

@isTest 
private class TestOnlyOneFund  
{
    static testMethod void validateFund()
    {
     Account a=new Account(Name='Test', AccountNumber='Test1');
     insert a;
     
    Fund__c c= new Fund__c(Fund_Name__c='test', Account__c=a.id);
     insert c;
    
    }
}



Please help what code I should add to increase test coverage for this class.

 

Thanks.

vishal@forcevishal@force

You have only created one CDFI Fund Record, so it doesn't go beyond count > 0 when the trigger is executed from the test coverage.

 

Simply create another record for CDFI Fund and then you'll be able to cover most of your code.

Amn12345Amn12345

Hi Vishal,

 

I tried adding another record too but its giving me error which is from my trigger itself. i.e. " There can be only one record of CDFI Fund and its already existing".

 

Any other workaround for the same?

APathakAPathak

This is the normal behavior of the trigger posted. To check the error put the second insert statement inside try-catch block.