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
RIteshMRIteshM 

Some Testing Doubts

i am trying to test my Controller .One doubt is in using Test.startTet and Test.stopTest().i am checking every method in separate unit test . for example

static testmethod void testEditApiSettings(){
         MirrorTestUtility.getApiSettings();
    Test.startTest();
    MirrorSettingsController ctrl = new MirrorSettingsController();   
  ctrl.setEditApiSettings();
  Test.stopTest();
System.assert(ctrl.editApiSettings,'Method perform to Show Insert view# Not able to set ');
}
static testmethod void testDeleteApiSettings(){
MirrorTestUtility.setUpSettings();
Test.startTest();
MirrorSettingsController ctrl = new MirrorSettingsController();
ctrl.deleteUserSettings();
Test.stopTest();
 GlassUserApiSettings__c userSettings =  GlassUserApiSettings__c.getValues(UserInfo.getUserId());
System.assert(userSettings== null ,'User settings are present ence Method don\'t delete Usetr Settings' );
}

so you can see i create ctrl every time in test.startTest() is this more appropriate or creating it before Test.startTest();

  1. there is a DML exception perhaps that cant come because DML limit is 100 and i am hardly using 2-3 in my controller. i write a function

    public PageReference deleteUserSettings(){
    try{  
    delete userSettings;  
    userSettings = GlassUserApiSettings__c.getValues(Userinfo.getUserId());
    }
    catch(System.DmlException e){System.debug(e); }
    return null;
     }

    how to test this should i remove this try catch block (because exception is not expected)??How to test this catch block please guideline .

IspitaIspita
Well adding any exception of if else branching will necessiate replication of scenario via data creation in test class, otherwise coverage willgo down.
If you think you can live without this exception you can remove to increase coverage or add other exceptions which you can generate and catch via code in apex test class to incraese coverage.
hope this answers your query!
regards,