I added a new method on a trigger that before update on a lead, checks to see if the user is of a specific profile. If they are, it renders an error. It works fine, but how can I write a unit test for this?
@isTest static void testChangingLeadStatus(){
Profile profItem = [Select ID, Name
From Profile
Where Name = 'X Profile'];
User u = CreateThisUser(profItem);
insert u;
Lead l = new Lead();
l.LastName = 'Test Last';
l.Company = 'Test Company';
l.State = 'CA';
l.Country = 'United States';
l.Status = 'status_a';
insert l;
Test.StartTest();
system.runAs(u){
l.Status = 'status_b';
update l;
}
Test.StopTest();
system.assertNotEquals(l.Status, 'status_b');
}
When I run this currently, it gives a DmlException, that I believe is due to the functional prevention of insert... the intended functionality.
System.DmlException: Update failed. First exception on row 0 with id 00Q7A000003D9WERC; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id:
When I comment out the little bit within the runAs block, it passes, but that doesn't really test anything.
How can I run a unit test to ensure that a user cannot update a field that is prevented via before update trigger?
.addError
?