You need to sign in to do that
Don't have an account?
prb
Simulate a profile in a trigger unit test
I have a trigger on the Contract object. The contract object has a validation rule that only allows certain profiles to add a record. My unit test fails for the trigger even though Im running the test under an administrative account.
Is there a way to add the unit test profile to the validation rule?
This doesn't seem right.
What seems better is to somehow simulate a mock profile that would have permission to pass the test.
Could someone please offer some guidance on the best practice?
Thanks
I think the actual problem is with your validation rule. Does it work correctly if you add the object via the standard interface, without the trigger?
I think that rule would always fail,because you have all of the conditions "and" together, and there is no way for the profileid to be 2 different values. when the profile id does equal 00e30000000e95EAAQ then the $Profile.Id <> '00e30000000e95FAAQ',would be true, and the error would fire.
you probably want something like this:
AND( ISNEW(), NOT(OR($Profile.Id == '00e30000000e95EAAQ', $Profile.Id == '00e30000000e95FAAQ')) )
All Answers
You can select create a User with the profile you would like to test and run the testmode using this users permission
Profile profile = [select id from profile where name='x']; User user = new User(alias = 'test', email='test@test.com', emailencodingkey='UTF-8', lastname='test', languagelocalekey='en_US', localesidkey='en_US', profileid = profile .Id, timezonesidkey='America/Los_Angeles', username='test@testSF.com'); System.runAs(user) { //do whatever you would like to thest
}
This is exactly the type of solution I was looking for.
Unfortunately this doesn't work.
Here's my validation rule:
AND(
ISNEW(),
$Profile.Id <> '00e30000000e95EAAQ',
$Profile.Id <> '00e30000000e95FAAQ',
)
the code I tried was:
User user = new User(alias = 'tester', email='tester420@test.com',emailencodingkey=
'UTF-8', lastname='test', languagelocalekey='en_US',localesidkey='en_US', profileid = profile.Id,timezonesidkey=
'America/New_York', username='testSF@test.com');system.debug('PMN: profile.Id =>'+profile.Id);
System.runAs(user) {
//my insert fails here w/validation error
}
Does anyone have any idea why the runAs user doesn't pass this validation test?
The validation is failing.
FIELD_CUSTOM_VALIDATION_EXCEPTION, "My validation message"
I think the actual problem is with your validation rule. Does it work correctly if you add the object via the standard interface, without the trigger?
I think that rule would always fail,because you have all of the conditions "and" together, and there is no way for the profileid to be 2 different values. when the profile id does equal 00e30000000e95EAAQ then the $Profile.Id <> '00e30000000e95FAAQ',would be true, and the error would fire.
you probably want something like this:
AND( ISNEW(), NOT(OR($Profile.Id == '00e30000000e95EAAQ', $Profile.Id == '00e30000000e95FAAQ')) )
This was indeed the problem.
Thanks everyone for your help.