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
TilluTillu 

Need Test Class For this trigger?

trigger trgr_Case_CaseCommentLock on CaseComment (before Update, before Delete) {
   
    List<Profile> currentUserProfile = new List<Profile>();
   
    currentUserProfile =
        [SELECT Name FROM Profile WHERE Id = :UserInfo.getProfileId() LIMIT 1];
   
    if(
        !currentUserProfile.isEmpty() &&
        currentUserProfile[0].Name != 'System Administrator'
    )
        if(trigger.isDelete)
            for(CaseComment cm : trigger.old)
                cm.addError('You cannot Delete a Case Comment!');
        else
            for(CaseComment cm : trigger.new)
                cm.addError('You cannot Edit a Case Comment!');       
}




I have tried some thing but not worked.

the code which i have tried...

@isTest
public class Test_CaseComments{

public static testMethod void main(){

//List<profile> Li = [SELECT Name FROM Profile WHERE Id = :UserInfo.getProfileId() LIMIT 1];
CaseComment cs = New CaseComment ();

        cs.name = 'TestOne';
        cs.Status = 'Open';  (picklist type)
        cs.Description = 'Test Case for test';
        cs.Origin = 'Annuity External'; (picklist type)
        cs.Type = 'Feature Request'; (picklist)
        cs.Business_Impact__c = 'No Impact';
        cs.Priority = 'Low'; (picklist)
     
        insert cs;
}
Best Answer chosen by Tillu
Carolina Ruiz MedinaCarolina Ruiz Medina
Hi Tillu,
I think your issue is that in your test you only insert but don't update. However your test is for : before Update, before Delete.

Then if in your test you do something like this :


.....
   cs.Priority = 'Low'; (picklist)
   insert cs;

   cs.Description = 'Udating';
   update cs;

// and now the delete option
   delete cs;

Also I would like to share with you some links and video that I think for testing would help :
Test and Testability : https://www.youtube.com/watch?v=dWertK6Legc
Unit testing with the domain layer : http://andyinthecloud.com/2014/03/23/unit-testing-with-the-domain-layer/

Hope it helps :-) 

Kind Regards,
Carolina.